home *** CD-ROM | disk | FTP | other *** search
/ MegaDoom Adventures / PMWMEGADOOM.iso / doom / creators / deu52gcc / src / objects.c < prev    next >
C/C++ Source or Header  |  1994-05-21  |  72KB  |  2,502 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.    OBJECTS.C - object handling routines.
  11. */
  12.  
  13. /* the includes */
  14. #include "deu.h"
  15. #include "levels.h"
  16.  
  17.  
  18. /*
  19.    highlight the selected objects
  20. */
  21.  
  22. void HighlightSelection( BCINT objtype, SelPtr list) /* SWAP! */
  23. {
  24.    SelPtr cur;
  25.  
  26.    if (list == NULL)
  27.       return;
  28.    for (cur = list; cur; cur = cur->next)
  29.       HighlightObject( objtype, cur->objnum, GREEN);
  30. }
  31.  
  32.  
  33.  
  34. /*
  35.    test if an object is in the selection list
  36. */
  37.  
  38. Bool IsSelected( SelPtr list, BCINT objnum)
  39. {
  40.    SelPtr cur;
  41.  
  42.    if (list == NULL)
  43.       return FALSE;
  44.    for (cur = list; cur; cur = cur->next)
  45.       if (cur->objnum == objnum)
  46.      return TRUE;
  47.    return FALSE;
  48. }
  49.  
  50.  
  51.  
  52. /*
  53.    add an object to the selection list
  54. */
  55.  
  56. void SelectObject( SelPtr *list, BCINT objnum)
  57. {
  58.    SelPtr cur;
  59.  
  60.    if (objnum < 0)
  61.       ProgError( "BUG: SelectObject called with %d", objnum);
  62.    cur = (SelPtr) GetMemory( sizeof( struct SelectionList));
  63.    cur->next = *list;
  64.    cur->objnum = objnum;
  65.    *list = cur;
  66. }
  67.  
  68.  
  69.  
  70. /*
  71.    remove an object from the selection list
  72. */
  73.  
  74. void UnSelectObject( SelPtr *list, BCINT objnum)
  75. {
  76.    SelPtr cur, prev;
  77.  
  78.    if (objnum < 0)
  79.       ProgError( "BUG: UnSelectObject called with %d", objnum);
  80.    prev = NULL;
  81.    cur = *list;
  82.    while (cur)
  83.    {
  84.       if (cur->objnum == objnum)
  85.       {
  86.      if (prev)
  87.         prev->next = cur->next;
  88.      else
  89.         *list = cur->next;
  90.      FreeMemory( cur);
  91.      if (prev)
  92.         cur = prev->next;
  93.      else
  94.         cur = *list;
  95.       }
  96.       else
  97.       {
  98.      prev = cur;
  99.      cur = cur->next;
  100.       }
  101.    }
  102. }
  103.  
  104.  
  105.  
  106. /*
  107.    forget the selection list
  108. */
  109.  
  110. void ForgetSelection( SelPtr *list)
  111. {
  112.    SelPtr cur, prev;
  113.  
  114.    cur = *list;
  115.    while (cur)
  116.    {
  117.       prev = cur;
  118.       cur = cur->next;
  119.       FreeMemory( prev);
  120.    }
  121.    *list = NULL;
  122. }
  123.  
  124.  
  125.  
  126. /*
  127.    get the number of objets of a given type minus one
  128. */
  129. BCINT GetMaxObjectNum( BCINT objtype)
  130. {
  131.    switch (objtype)
  132.    {
  133.    case OBJ_THINGS:
  134.       return NumThings - 1;
  135.    case OBJ_LINEDEFS:
  136.       return NumLineDefs - 1;
  137.    case OBJ_SIDEDEFS:
  138.       return NumSideDefs - 1;
  139.    case OBJ_VERTEXES:
  140.       return NumVertexes - 1;
  141.    case OBJ_SEGS:
  142.       return NumSegs - 1;
  143.    case OBJ_SSECTORS:
  144.       return NumSSectors - 1;
  145.    case OBJ_SECTORS:
  146.       return NumSectors - 1;
  147.    }
  148.    return -1;
  149. }
  150.  
  151.  
  152. /*
  153.    check if there is something of interest inside the given box
  154. */
  155.  
  156. BCINT GetCurObject( BCINT objtype, BCINT x0, BCINT y0, BCINT x1, BCINT y1) /* SWAP! */
  157. {
  158.    BCINT n, m, cur, curx;
  159.    BCINT lx0, ly0, lx1, ly1;
  160.    BCINT midx, midy;
  161.  
  162.    cur = -1;
  163.    if (x1 < x0)
  164.    {
  165.       n = x0;
  166.       x0 = x1;
  167.       x1 = n;
  168.    }
  169.    if (y1 < y0)
  170.    {
  171.       n = y0;
  172.       y0 = y1;
  173.       y1 = n;
  174.    }
  175.  
  176.    switch (objtype)
  177.    {
  178.    case OBJ_THINGS:
  179.       ObjectsNeeded( OBJ_THINGS, 0);
  180.       for (n = 0; n < NumThings; n++)
  181.           if (Things[ n].xpos >= x0 && Things[ n].xpos <= x1 && Things[ n].ypos >= y0 && Things[ n].ypos <= y1)
  182.           {
  183.              cur = n;
  184.              break;
  185.           }
  186.       break;
  187.    case OBJ_VERTEXES:
  188.       ObjectsNeeded( OBJ_VERTEXES, 0);
  189.       for (n = 0; n < NumVertexes; n++)
  190.           if (Vertexes[ n].x >= x0 && Vertexes[ n].x <= x1 && Vertexes[ n].y >= y0 && Vertexes[ n].y <= y1)
  191.           {
  192.              cur = n;
  193.              break;
  194.           }
  195.       break;
  196.    case OBJ_LINEDEFS:
  197.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  198.       for (n = 0; n < NumLineDefs; n++)
  199.       {
  200.           if (IsLineDefInside( n, x0, y0, x1, y1))
  201.           {
  202.              cur = n;
  203.              break;
  204.           }
  205.       }
  206.       break;
  207.    case OBJ_SECTORS:
  208.       /* hack, hack...  I look for the first LineDef crossing an horizontal half-line drawn from the cursor */
  209.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  210.       curx = MapMaxX + 1;
  211.       cur = -1;
  212.       midx = (x0 + x1) / 2;
  213.       midy = (y0 + y1) / 2;
  214.       for (n = 0; n < NumLineDefs; n++)
  215.          if ((Vertexes[ LineDefs[ n].start].y > midy) != (Vertexes[ LineDefs[ n].end].y > midy))
  216.           {
  217.              lx0 = Vertexes[ LineDefs[ n].start].x;
  218.              ly0 = Vertexes[ LineDefs[ n].start].y;
  219.              lx1 = Vertexes[ LineDefs[ n].end].x;
  220.              ly1 = Vertexes[ LineDefs[ n].end].y;
  221.              m = lx0 + (BCINT) ((long) (midy - ly0) * (long) (lx1 - lx0) / (long) (ly1 - ly0));
  222.              if (m >= midx && m < curx)
  223.              {
  224.                 curx = m;
  225.                 cur = n;
  226.              }
  227.           }
  228.       /* now look if this LineDef has a SideDef bound to one sector */
  229.       if (cur >= 0)
  230.       {
  231.           if (Vertexes[ LineDefs[ cur].start].y > Vertexes[ LineDefs[ cur].end].y)
  232.              cur = LineDefs[ cur].sidedef1;
  233.           else
  234.              cur = LineDefs[ cur].sidedef2;
  235.           if (cur >= 0)
  236.           {
  237.              ObjectsNeeded( OBJ_SIDEDEFS, 0);
  238.              cur = SideDefs[ cur].sector;
  239.           }
  240.           else
  241.              cur = -1;
  242.       }
  243.       else
  244.           cur = -1;
  245.       break;
  246.    }
  247.    return cur;
  248. }
  249.  
  250.  
  251.  
  252. /*
  253.    select all objects inside a given box
  254. */
  255.  
  256. SelPtr SelectObjectsInBox( BCINT objtype, BCINT x0, BCINT y0, BCINT x1, BCINT y1) /* SWAP! */
  257. {
  258.    BCINT n, m;
  259.    SelPtr list;
  260.  
  261.    list = NULL;
  262.    if (x1 < x0)
  263.    {
  264.       n = x0;
  265.       x0 = x1;
  266.       x1 = n;
  267.    }
  268.    if (y1 < y0)
  269.    {
  270.       n = y0;
  271.       y0 = y1;
  272.       y1 = n;
  273.    }
  274.  
  275.    switch (objtype)
  276.    {
  277.    case OBJ_THINGS:
  278.       ObjectsNeeded( OBJ_THINGS, 0);
  279.       for (n = 0; n < NumThings; n++)
  280.      if (Things[ n].xpos >= x0 && Things[ n].xpos <= x1 && Things[ n].ypos >= y0 && Things[ n].ypos <= y1)
  281.         SelectObject( &list, n);
  282.       break;
  283.    case OBJ_VERTEXES:
  284.       ObjectsNeeded( OBJ_VERTEXES, 0);
  285.       for (n = 0; n < NumVertexes; n++)
  286.      if (Vertexes[ n].x >= x0 && Vertexes[ n].x <= x1 && Vertexes[ n].y >= y0 && Vertexes[ n].y <= y1)
  287.         SelectObject( &list, n);
  288.       break;
  289.    case OBJ_LINEDEFS:
  290.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  291.       for (n = 0; n < NumLineDefs; n++)
  292.       {
  293.      /* the two ends of the line must be in the box */
  294.      m = LineDefs[ n].start;
  295.      if (Vertexes[ m].x < x0 || Vertexes[ m].x > x1 || Vertexes[ m].y < y0 || Vertexes[ m].y > y1)
  296.         continue;
  297.      m = LineDefs[ n].end;
  298.      if (Vertexes[ m].x < x0 || Vertexes[ m].x > x1 || Vertexes[ m].y < y0 || Vertexes[ m].y > y1)
  299.         continue;
  300.      SelectObject( &list, n);
  301.       }
  302.       break;
  303.    case OBJ_SECTORS:
  304.       /* hack: select all sectors... */
  305.       for (n = 0; n < NumSectors; n++)
  306.       SelectObject( &list, n);
  307.       /* ... then remove the unwanted ones from the list */
  308.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, OBJ_VERTEXES, 0);
  309.       for (n = 0; n < NumLineDefs; n++)
  310.       {
  311.      m = LineDefs[ n].start;
  312.      if (Vertexes[ m].x < x0 || Vertexes[ m].x > x1 || Vertexes[ m].y < y0 || Vertexes[ m].y > y1)
  313.      {
  314.         m = LineDefs[ n].sidedef1;
  315.         if (m >= 0 && SideDefs[ m].sector >= 0)
  316.            UnSelectObject( &list, SideDefs[ m].sector);
  317.         m = LineDefs[ n].sidedef2;
  318.         if (m >= 0 && SideDefs[ m].sector >= 0)
  319.            UnSelectObject( &list, SideDefs[ m].sector);
  320.         continue;
  321.      }
  322.      m = LineDefs[ n].end;
  323.      if (Vertexes[ m].x < x0 || Vertexes[ m].x > x1 || Vertexes[ m].y < y0 || Vertexes[ m].y > y1)
  324.      {
  325.         m = LineDefs[ n].sidedef1;
  326.         if (m >= 0 && SideDefs[ m].sector >= 0)
  327.            UnSelectObject( &list, SideDefs[ m].sector);
  328.         m = LineDefs[ n].sidedef2;
  329.         if (m >= 0 && SideDefs[ m].sector >= 0)
  330.            UnSelectObject( &list, SideDefs[ m].sector);
  331.         continue;
  332.      }
  333.       }
  334.       break;
  335.    }
  336.    return list;
  337. }
  338.  
  339.  
  340.  
  341. /*
  342.    highlight the selected object
  343. */
  344.  
  345. void HighlightObject( BCINT objtype, BCINT objnum, BCINT color) /* SWAP! */
  346. {
  347.    BCINT  n, m;
  348.  
  349.    /* use XOR mode : drawing any line twice erases it */
  350.    setwritemode( XOR_PUT);
  351.    SetColor( color);
  352.    switch ( objtype)
  353.    {
  354.    case OBJ_THINGS:
  355.       ObjectsNeeded( OBJ_THINGS, 0);
  356.       m = (GetThingRadius( Things[ objnum].type) * 3) / 2;
  357.       DrawMapLine( Things[ objnum].xpos - m, Things[ objnum].ypos - m, Things[ objnum].xpos - m, Things[ objnum].ypos + m);
  358.       DrawMapLine( Things[ objnum].xpos - m, Things[ objnum].ypos + m, Things[ objnum].xpos + m, Things[ objnum].ypos + m);
  359.       DrawMapLine( Things[ objnum].xpos + m, Things[ objnum].ypos + m, Things[ objnum].xpos + m, Things[ objnum].ypos - m);
  360.       DrawMapLine( Things[ objnum].xpos + m, Things[ objnum].ypos - m, Things[ objnum].xpos - m, Things[ objnum].ypos - m);
  361.       DrawMapArrow( Things[ objnum].xpos, Things[ objnum].ypos, Things[ objnum].angle * 182);
  362.       break;
  363.    case OBJ_LINEDEFS:
  364.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  365.       n = (Vertexes[ LineDefs[ objnum].start].x + Vertexes[ LineDefs[ objnum].end].x) / 2;
  366.       m = (Vertexes[ LineDefs[ objnum].start].y + Vertexes[ LineDefs[ objnum].end].y) / 2;
  367.       DrawMapLine( n, m, n + (Vertexes[ LineDefs[ objnum].end].y - Vertexes[ LineDefs[ objnum].start].y) / 3, m + (Vertexes[ LineDefs[ objnum].start].x - Vertexes[ LineDefs[ objnum].end].x) / 3);
  368.       setlinestyle(SOLID_LINE, 0, THICK_WIDTH);
  369.       DrawMapVector( Vertexes[ LineDefs[ objnum].start].x, Vertexes[ LineDefs[ objnum].start].y,
  370.              Vertexes[ LineDefs[ objnum].end].x, Vertexes[ LineDefs[ objnum].end].y);
  371.       if (color != LIGHTRED && LineDefs[ objnum].tag > 0)
  372.       {
  373.      for (m = 0; m < NumSectors; m++)
  374.         if (Sectors[ m].tag == LineDefs[ objnum].tag)
  375.            HighlightObject( OBJ_SECTORS, m, LIGHTRED);
  376.       }
  377.       setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
  378.       break;
  379.    case OBJ_VERTEXES:
  380.       ObjectsNeeded( OBJ_VERTEXES, 0);
  381.       DrawMapLine( Vertexes[ objnum].x - OBJSIZE * 2, Vertexes[ objnum].y - OBJSIZE * 2, Vertexes[ objnum].x - OBJSIZE * 2, Vertexes[ objnum].y + OBJSIZE * 2);
  382.       DrawMapLine( Vertexes[ objnum].x - OBJSIZE * 2, Vertexes[ objnum].y + OBJSIZE * 2, Vertexes[ objnum].x + OBJSIZE * 2, Vertexes[ objnum].y + OBJSIZE * 2);
  383.       DrawMapLine( Vertexes[ objnum].x + OBJSIZE * 2, Vertexes[ objnum].y + OBJSIZE * 2, Vertexes[ objnum].x + OBJSIZE * 2, Vertexes[ objnum].y - OBJSIZE * 2);
  384.       DrawMapLine( Vertexes[ objnum].x + OBJSIZE * 2, Vertexes[ objnum].y - OBJSIZE * 2, Vertexes[ objnum].x - OBJSIZE * 2, Vertexes[ objnum].y - OBJSIZE * 2);
  385.       break;
  386.    case OBJ_SECTORS:
  387.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, OBJ_VERTEXES, 0);
  388.       setlinestyle(SOLID_LINE, 0, THICK_WIDTH);
  389.       for (n = 0; n < NumLineDefs; n++)
  390.      if ( (LineDefs[n].sidedef1>=0 && SideDefs[LineDefs[n].sidedef1].sector == objnum ) ||
  391.               (LineDefs[n].sidedef2>=0 && SideDefs[LineDefs[n].sidedef2].sector == objnum ))
  392.         DrawMapLine( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  393.                       Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  394.       if (color != LIGHTRED && Sectors[ objnum].tag > 0)
  395.       {
  396.        for (m = 0; m < NumLineDefs; m++)
  397.           if (LineDefs[ m].tag == Sectors[ objnum].tag)
  398.              HighlightObject( OBJ_LINEDEFS, m, LIGHTRED);
  399.       } 
  400.       setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
  401.       break;
  402.    }
  403.    /* restore normal write mode */
  404.    setwritemode( COPY_PUT);
  405. }
  406.  
  407.  
  408.  
  409. /*
  410.    delete an object
  411. */
  412.  
  413. void DeleteObject( BCINT objtype, BCINT objnum) /* SWAP! */
  414. {
  415.    SelPtr list;
  416.  
  417.    list = NULL;
  418.    SelectObject( &list, objnum);
  419.    DeleteObjects( objtype, &list);
  420. }
  421.  
  422.  
  423.  
  424. /*
  425.    delete a group of objects (*recursive*)
  426. */
  427.  
  428. void DeleteObjects( BCINT objtype, SelPtr *list) /* SWAP! */
  429. {
  430.    BCINT    n, objnum;
  431.    SelPtr cur;
  432.  
  433.    MadeChanges = TRUE;
  434.    switch (objtype)
  435.    {
  436.    case OBJ_THINGS:
  437.       ObjectsNeeded( OBJ_THINGS, 0);
  438.       while (*list)
  439.       {
  440.      objnum = (*list)->objnum;
  441.      /* delete the Thing */
  442.      NumThings--;
  443.      if (NumThings > 0)
  444.      {
  445.         for (n = objnum; n < NumThings; n++)
  446.            Things[ n] = Things[ n + 1];
  447.         Things = (TPtr) ResizeFarMemory( Things, NumThings * sizeof( struct Thing));
  448.      }
  449.      else
  450.      {
  451.         FreeFarMemory( Things);
  452.         Things = NULL;
  453.      }
  454.      for (cur = (*list)->next; cur; cur = cur->next)
  455.         if (cur->objnum > objnum)
  456.            cur->objnum--;
  457.      UnSelectObject( list, objnum);
  458.       }
  459.       break;
  460.    case OBJ_VERTEXES:
  461.       while (*list)
  462.       {
  463.      objnum = (*list)->objnum;
  464.      /* delete the LineDefs bound to this Vertex and change the references */
  465.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  466.      for (n = 0; n < NumLineDefs; n++)
  467.      {
  468.         if (LineDefs[ n].start == objnum || LineDefs[ n].end == objnum)
  469.            DeleteObject( OBJ_LINEDEFS, n--);
  470.         else
  471.         {
  472.            if (LineDefs[ n].start >= objnum)
  473.           LineDefs[ n].start--;
  474.            if (LineDefs[ n].end >= objnum)
  475.           LineDefs[ n].end--;
  476.         }
  477.      }
  478.      /* delete the Vertex */
  479.      ObjectsNeeded( OBJ_VERTEXES, 0);
  480.      NumVertexes--;
  481.      if (NumVertexes > 0)
  482.      {
  483.         for (n = objnum; n < NumVertexes; n++)
  484.            Vertexes[ n] = Vertexes[ n + 1];
  485.         Vertexes = (VPtr) ResizeFarMemory( Vertexes, NumVertexes * sizeof( struct Vertex));
  486.      }
  487.      else
  488.      {
  489.         FreeFarMemory( Vertexes);
  490.         Vertexes = NULL;
  491.      }
  492.      for (cur = (*list)->next; cur; cur = cur->next)
  493.         if (cur->objnum > objnum)
  494.            cur->objnum--;
  495.      UnSelectObject( list, objnum);
  496.       }
  497.       break;
  498.    case OBJ_LINEDEFS:
  499.       while (*list)
  500.       {
  501.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  502.      objnum = (*list)->objnum;
  503.      /* delete the two SideDefs bound to this LineDef */
  504.      if (LineDefs[ objnum].sidedef1 >= 0)
  505.      {
  506.         DeleteObject( OBJ_SIDEDEFS, LineDefs[ objnum].sidedef1);
  507.         ObjectsNeeded( OBJ_LINEDEFS, 0);
  508.      }
  509.      if (LineDefs[ objnum].sidedef2 >= 0)
  510.      {
  511.         DeleteObject( OBJ_SIDEDEFS, LineDefs[ objnum].sidedef2);
  512.         ObjectsNeeded( OBJ_LINEDEFS, 0);
  513.      }
  514.      /* delete the LineDef */
  515.      NumLineDefs--;
  516.      if (NumLineDefs > 0)
  517.      {
  518.         for (n = objnum; n < NumLineDefs; n++)
  519.            LineDefs[ n] = LineDefs[ n + 1];
  520.         LineDefs = (LDPtr) ResizeFarMemory( LineDefs, NumLineDefs * sizeof( struct LineDef));
  521.      }
  522.      else
  523.      {
  524.         FreeFarMemory( LineDefs);
  525.         LineDefs = NULL;
  526.      }
  527.      for (cur = (*list)->next; cur; cur = cur->next)
  528.         if (cur->objnum > objnum)
  529.            cur->objnum--;
  530.      UnSelectObject( list, objnum);
  531.       }
  532.       break;
  533.    case OBJ_SIDEDEFS:
  534.       while (*list)
  535.       {
  536.      objnum = (*list)->objnum;
  537.      /* change the LineDefs references */
  538.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  539.      for (n = 0; n < NumLineDefs; n++)
  540.      {
  541.         if (LineDefs[ n].sidedef1 == objnum)
  542.            LineDefs[ n].sidedef1 = -1;
  543.         else if (LineDefs[ n].sidedef1 >= objnum)
  544.            LineDefs[ n].sidedef1--;
  545.         if (LineDefs[ n].sidedef2 == objnum)
  546.            LineDefs[ n].sidedef2 = -1;
  547.         else if (LineDefs[ n].sidedef2 >= objnum)
  548.            LineDefs[ n].sidedef2--;
  549.      }
  550.      /* delete the SideDef */
  551.      ObjectsNeeded( OBJ_SIDEDEFS, 0);
  552.      NumSideDefs--;
  553.      if (NumSideDefs > 0)
  554.      {
  555.         for (n = objnum; n < NumSideDefs; n++)
  556.            SideDefs[ n] = SideDefs[ n + 1];
  557.         SideDefs = (SDPtr) ResizeFarMemory( SideDefs, NumSideDefs * sizeof( struct SideDef));
  558.      }
  559.      else
  560.      {
  561.         FreeFarMemory( SideDefs);
  562.         SideDefs = NULL;
  563.      }
  564.      for (cur = (*list)->next; cur; cur = cur->next)
  565.         if (cur->objnum > objnum)
  566.            cur->objnum--;
  567.      UnSelectObject( list, objnum);
  568.       }
  569.       MadeMapChanges = TRUE;
  570.       break;
  571.    case OBJ_SECTORS:
  572.       while (*list)
  573.       {
  574.      objnum = (*list)->objnum;
  575.      /* delete the SideDefs bound to this Sector and change the references */
  576.      ObjectsNeeded( OBJ_SIDEDEFS, 0);
  577.      for (n = 0; n < NumSideDefs; n++)
  578.         if (SideDefs[ n].sector == objnum)
  579.            DeleteObject( OBJ_SIDEDEFS, n--);
  580.         else if (SideDefs[ n].sector >= objnum)
  581.            SideDefs[ n].sector--;
  582.      /* delete the Sector */
  583.      ObjectsNeeded( OBJ_SECTORS, 0);
  584.      NumSectors--;
  585.      if (NumSectors > 0)
  586.      {
  587.         for (n = objnum; n < NumSectors; n++)
  588.            Sectors[ n] = Sectors[ n + 1];
  589.         Sectors = (SPtr) ResizeFarMemory( Sectors, NumSectors * sizeof( struct Sector));
  590.      }
  591.      else
  592.      {
  593.         FreeFarMemory( Sectors);
  594.         Sectors = NULL;
  595.      }
  596.      for (cur = (*list)->next; cur; cur = cur->next)
  597.         if (cur->objnum > objnum)
  598.            cur->objnum--;
  599.      UnSelectObject( list, objnum);
  600.       }
  601.       break;
  602.    default:
  603.       Beep();
  604.    }
  605. }
  606.  
  607.  
  608.  
  609. /*
  610.    insert a new object
  611. */
  612.  
  613. void InsertObject(BCINT objtype, BCINT copyfrom, BCINT xpos, BCINT ypos) /* SWAP! */
  614. {
  615.    BCINT last;
  616.  
  617.    ObjectsNeeded( objtype, 0);
  618.    MadeChanges = TRUE;
  619.    switch (objtype)
  620.    {
  621.    case OBJ_THINGS:
  622.       last = NumThings++;
  623.       if (last > 0)
  624.      Things = (TPtr) ResizeFarMemory( Things, (unsigned long) NumThings * sizeof( struct Thing));
  625.       else
  626.            Things = (TPtr) GetFarMemory( sizeof( struct Thing));
  627.       Things[ last].xpos = xpos;
  628.       Things[ last].ypos = ypos;
  629.       if (copyfrom >= 0)
  630.       {
  631.      Things[ last].type  = Things[ copyfrom].type;
  632.      Things[ last].angle = Things[ copyfrom].angle;
  633.      Things[ last].when  = Things[ copyfrom].when;
  634.       }
  635.       else
  636.       {
  637.      Things[ last].type  = THING_TROOPER;
  638.      Things[ last].angle = 0;
  639.            Things[ last].when  = 0x07;
  640.       }
  641.       break;
  642.    case OBJ_VERTEXES:
  643.       last = NumVertexes++;
  644.       if (last > 0)
  645.            Vertexes = (VPtr) ResizeFarMemory( Vertexes, (unsigned long) NumVertexes * sizeof( struct Vertex));
  646.       else
  647.       Vertexes = (VPtr) GetFarMemory( sizeof( struct Vertex));
  648.       /* kluge: the Nodes builder will put -2 in copyfrom */
  649.       if (copyfrom == -2)
  650.       {
  651.         Vertexes[ last].x = xpos;
  652.         Vertexes[ last].y = ypos;
  653.       }
  654.       else
  655.       {
  656.         Vertexes[ last].x = xpos & ~7;
  657.           Vertexes[ last].y = ypos & ~7;
  658.           if (Vertexes[ last].x < MapMinX)
  659.               MapMinX = Vertexes[ last].x;
  660.           if (Vertexes[ last].x > MapMaxX)
  661.               MapMaxX = Vertexes[ last].x;
  662.           if (Vertexes[ last].y < MapMinY)
  663.               MapMinY = Vertexes[ last].y;
  664.           if (Vertexes[ last].y > MapMaxY)
  665.               MapMaxY = Vertexes[ last].y;
  666.           MadeMapChanges = TRUE;
  667.       }
  668.       break;
  669.    case OBJ_LINEDEFS:
  670.       last = NumLineDefs++;
  671.       if (last > 0)
  672.            LineDefs = (LDPtr) ResizeFarMemory( LineDefs, (unsigned long) NumLineDefs * sizeof( struct LineDef));
  673.       else
  674.            LineDefs = (LDPtr) GetFarMemory( sizeof( struct LineDef));
  675.       if (copyfrom >= 0)
  676.       {
  677.            LineDefs[ last].start = LineDefs[ copyfrom].start;
  678.            LineDefs[ last].end = LineDefs[ copyfrom].end;
  679.            LineDefs[ last].flags = LineDefs[ copyfrom].flags;
  680.            LineDefs[ last].type = LineDefs[ copyfrom].type;
  681.            LineDefs[ last].tag = LineDefs[ copyfrom].tag;
  682.       }
  683.       else
  684.       {
  685.            LineDefs[ last].start = 0;
  686.            LineDefs[ last].end = NumVertexes - 1;
  687.            LineDefs[ last].flags = 1;
  688.            LineDefs[ last].type = 0;
  689.            LineDefs[ last].tag = 0;
  690.       }
  691.       LineDefs[ last].sidedef1 = -1;
  692.       LineDefs[ last].sidedef2 = -1;
  693.       break;
  694.    case OBJ_SIDEDEFS:
  695.       /* SideDefs are added from the LineDefs menu, so "copyfrom" should always be -1.  But I test it anyway. */
  696.       last = NumSideDefs++;
  697.       if (last > 0)
  698.      SideDefs = (SDPtr) ResizeFarMemory( SideDefs, (unsigned long) NumSideDefs * sizeof( struct SideDef));
  699.       else
  700.      SideDefs = (SDPtr) GetFarMemory( sizeof( struct SideDef));
  701.       if (copyfrom >= 0)
  702.       {
  703.      SideDefs[ last].xoff = SideDefs[ copyfrom].xoff;
  704.      SideDefs[ last].yoff = SideDefs[ copyfrom].yoff;
  705.      strncpy( SideDefs[ last].tex1, SideDefs[ copyfrom].tex1, 8);
  706.      strncpy( SideDefs[ last].tex2, SideDefs[ copyfrom].tex2, 8);
  707.      strncpy( SideDefs[ last].tex3, SideDefs[ copyfrom].tex3, 8);
  708.      SideDefs[ last].sector = SideDefs[ copyfrom].sector;
  709.       }
  710.       else
  711.       {
  712.      SideDefs[ last].xoff = 0;
  713.      SideDefs[ last].yoff = 0;
  714.      strcpy( SideDefs[ last].tex1, "-");
  715.      strcpy( SideDefs[ last].tex2, "-");
  716.      strcpy( SideDefs[ last].tex3, DefaultWallTexture);
  717.      SideDefs[ last].sector = NumSectors - 1;
  718.       }
  719.       MadeMapChanges = TRUE;
  720.       break;
  721.    case OBJ_SECTORS:
  722.       last = NumSectors++;
  723.       if (last > 0)
  724.      Sectors = (SPtr) ResizeFarMemory( Sectors, (unsigned long) NumSectors * sizeof( struct Sector));
  725.       else
  726.      Sectors = (SPtr) GetFarMemory( sizeof( struct Sector));
  727.       if (copyfrom >= 0)
  728.       {
  729.      Sectors[ last].floorh = Sectors[ copyfrom].floorh;
  730.      Sectors[ last].ceilh = Sectors[ copyfrom].ceilh;
  731.      strncpy( Sectors[ last].floort, Sectors[ copyfrom].floort, 8);
  732.      strncpy( Sectors[ last].ceilt, Sectors[ copyfrom].ceilt, 8);
  733.      Sectors[ last].light = Sectors[ copyfrom].light;
  734.      Sectors[ last].special = Sectors[ copyfrom].special;
  735.      Sectors[ last].tag = Sectors[ copyfrom].tag;
  736.       }
  737.       else
  738.       {
  739.      Sectors[ last].floorh = DefaultFloorHeight;
  740.      Sectors[ last].ceilh = DefaultCeilingHeight;
  741.      strncpy( Sectors[ last].floort, DefaultFloorTexture, 8);
  742.      strncpy( Sectors[ last].ceilt, DefaultCeilingTexture, 8);
  743.      Sectors[ last].light = 255;
  744.      Sectors[ last].special = 0;
  745.      Sectors[ last].tag = 0;
  746.       }
  747.       break;
  748.    default:
  749.       Beep();
  750.    }
  751. }
  752.  
  753.  
  754.  
  755. /*
  756.    check if a (part of a) LineDef is inside a given block
  757. */
  758.  
  759. Bool IsLineDefInside( BCINT ldnum, BCINT x0, BCINT y0, BCINT x1, BCINT y1) /* SWAP - needs Vertexes & LineDefs */
  760. {
  761.    BCINT lx0 = Vertexes[ LineDefs[ ldnum].start].x;
  762.    BCINT ly0 = Vertexes[ LineDefs[ ldnum].start].y;
  763.    BCINT lx1 = Vertexes[ LineDefs[ ldnum].end].x;
  764.    BCINT ly1 = Vertexes[ LineDefs[ ldnum].end].y;
  765.    BCINT i;
  766.  
  767.    /* do you like mathematics? */
  768.    if (lx0 >= x0 && lx0 <= x1 && ly0 >= y0 && ly0 <= y1)
  769.       return TRUE; /* the LineDef start is entirely inside the square */
  770.    if (lx1 >= x0 && lx1 <= x1 && ly1 >= y0 && ly1 <= y1)
  771.       return TRUE; /* the LineDef end is entirely inside the square */
  772.    if ((ly0 > y0) != (ly1 > y0))
  773.    {
  774.       i = lx0 + (BCINT) ( (long) (y0 - ly0) * (long) (lx1 - lx0) / (long) (ly1 - ly0));
  775.       if (i >= x0 && i <= x1)
  776.      return TRUE; /* the LineDef crosses the y0 side (left) */
  777.    }
  778.    if ((ly0 > y1) != (ly1 > y1))
  779.    {
  780.       i = lx0 + (BCINT) ( (long) (y1 - ly0) * (long) (lx1 - lx0) / (long) (ly1 - ly0));
  781.       if (i >= x0 && i <= x1)
  782.      return TRUE; /* the LineDef crosses the y1 side (right) */
  783.    }
  784.    if ((lx0 > x0) != (lx1 > x0))
  785.    {
  786.       i = ly0 + (BCINT) ( (long) (x0 - lx0) * (long) (ly1 - ly0) / (long) (lx1 - lx0));
  787.       if (i >= y0 && i <= y1)
  788.      return TRUE; /* the LineDef crosses the x0 side (down) */
  789.    }
  790.    if ((lx0 > x1) != (lx1 > x1))
  791.    {
  792.       i = ly0 + (BCINT) ( (long) (x1 - lx0) * (long) (ly1 - ly0) / (long) (lx1 - lx0));
  793.       if (i >= y0 && i <= y1)
  794.      return TRUE; /* the LineDef crosses the x1 side (up) */
  795.    }
  796.    return FALSE;        
  797. }
  798.  
  799.  
  800.  
  801. /*
  802.    get the Sector number of the SideDef opposite to this SideDef
  803.    (returns -1 if it cannot be found)
  804. */
  805.  
  806. BCINT GetOppositeSector( BCINT ld1, Bool firstside) /* SWAP! */
  807. {
  808.    BCINT x0, y0, dx0, dy0;
  809.    BCINT x1, y1, dx1, dy1;
  810.    BCINT x2, y2, dx2, dy2;
  811.    BCINT ld2, dist;
  812.    BCINT bestld, bestdist, bestmdist;
  813.  
  814.    /* get the coords for this LineDef */
  815.    ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  816.    x0  = Vertexes[ LineDefs[ ld1].start].x;
  817.    y0  = Vertexes[ LineDefs[ ld1].start].y;
  818.    dx0 = Vertexes[ LineDefs[ ld1].end].x - x0;
  819.    dy0 = Vertexes[ LineDefs[ ld1].end].y - y0;
  820.  
  821.    /* find the normal vector for this LineDef */
  822.    x1  = (dx0 + x0 + x0) / 2;
  823.    y1  = (dy0 + y0 + y0) / 2;
  824.    if (firstside == TRUE)
  825.    {
  826.      dx1 = dy0;
  827.      dy1 = -dx0;
  828.    }
  829.    else
  830.    {
  831.      dx1 = -dy0;
  832.      dy1 = dx0;
  833.    }
  834.  
  835.    bestld = -1;
  836.    /* use a parallel to an axis instead of the normal vector (faster method) */
  837.    if (abs( dy1) > abs( dx1))
  838.    {
  839.       if (dy1 > 0)
  840.       {
  841.      /* get the nearest LineDef in that direction (increasing Y's: North) */
  842.      bestdist = 32767;
  843.      bestmdist = 32767;
  844.      for (ld2 = 0; ld2 < NumLineDefs; ld2++)
  845.         if (ld2 != ld1 && ((Vertexes[ LineDefs[ ld2].start].x > x1) != (Vertexes[ LineDefs[ ld2].end].x > x1)))
  846.         {
  847.            x2  = Vertexes[ LineDefs[ ld2].start].x;
  848.            y2  = Vertexes[ LineDefs[ ld2].start].y;
  849.            dx2 = Vertexes[ LineDefs[ ld2].end].x - x2;
  850.            dy2 = Vertexes[ LineDefs[ ld2].end].y - y2;
  851.            dist = y2 + (BCINT) ((long) (x1 - x2) * (long) dy2 / (long) dx2);
  852.            if (dist > y1 && (dist < bestdist || (dist == bestdist && (y2 + dy2 / 2) < bestmdist)))
  853.            {
  854.           bestld = ld2;
  855.           bestdist = dist;
  856.           bestmdist = y2 + dy2 / 2;
  857.            }
  858.         }
  859.       }
  860.       else
  861.       {
  862.      /* get the nearest LineDef in that direction (decreasing Y's: South) */
  863.      bestdist = -32767;
  864.      bestmdist = -32767;
  865.      for (ld2 = 0; ld2 < NumLineDefs; ld2++)
  866.         if (ld2 != ld1 && ((Vertexes[ LineDefs[ ld2].start].x > x1) != (Vertexes[ LineDefs[ ld2].end].x > x1)))
  867.         {
  868.            x2  = Vertexes[ LineDefs[ ld2].start].x;
  869.            y2  = Vertexes[ LineDefs[ ld2].start].y;
  870.            dx2 = Vertexes[ LineDefs[ ld2].end].x - x2;
  871.            dy2 = Vertexes[ LineDefs[ ld2].end].y - y2;
  872.            dist = y2 + (BCINT) ((long) (x1 - x2) * (long) dy2 / (long) dx2);
  873.            if (dist < y1 && (dist > bestdist || (dist == bestdist && (y2 + dy2 / 2) > bestmdist)))
  874.            {
  875.           bestld = ld2;
  876.           bestdist = dist;
  877.           bestmdist = y2 + dy2 / 2;
  878.            }
  879.         }
  880.       }
  881.    }
  882.    else
  883.    {
  884.       if (dx1 > 0)
  885.       {
  886.      /* get the nearest LineDef in that direction (increasing X's: East) */
  887.      bestdist = 32767;
  888.      bestmdist = 32767;
  889.      for (ld2 = 0; ld2 < NumLineDefs; ld2++)
  890.         if (ld2 != ld1 && ((Vertexes[ LineDefs[ ld2].start].y > y1) != (Vertexes[ LineDefs[ ld2].end].y > y1)))
  891.         {
  892.            x2  = Vertexes[ LineDefs[ ld2].start].x;
  893.            y2  = Vertexes[ LineDefs[ ld2].start].y;
  894.            dx2 = Vertexes[ LineDefs[ ld2].end].x - x2;
  895.            dy2 = Vertexes[ LineDefs[ ld2].end].y - y2;
  896.            dist = x2 + (BCINT) ((long) (y1 - y2) * (long) dx2 / (long) dy2);
  897.            if (dist > x1 && (dist < bestdist || (dist == bestdist && (x2 + dx2 / 2) < bestmdist)))
  898.            {
  899.           bestld = ld2;
  900.           bestdist = dist;
  901.           bestmdist = x2 + dx2 / 2;
  902.            }
  903.         }
  904.       }
  905.       else
  906.       {
  907.      /* get the nearest LineDef in that direction (decreasing X's: West) */
  908.      bestdist = -32767;
  909.      bestmdist = -32767;
  910.      for (ld2 = 0; ld2 < NumLineDefs; ld2++)
  911.         if (ld2 != ld1 && ((Vertexes[ LineDefs[ ld2].start].y > y1) != (Vertexes[ LineDefs[ ld2].end].y > y1)))
  912.         {
  913.            x2  = Vertexes[ LineDefs[ ld2].start].x;
  914.            y2  = Vertexes[ LineDefs[ ld2].start].y;
  915.            dx2 = Vertexes[ LineDefs[ ld2].end].x - x2;
  916.            dy2 = Vertexes[ LineDefs[ ld2].end].y - y2;
  917.            dist = x2 + (BCINT) ((long) (y1 - y2) * (long) dx2 / (long) dy2);
  918.            if (dist < x1 && (dist > bestdist || (dist == bestdist && (x2 + dx2 / 2) > bestmdist)))
  919.            {
  920.           bestld = ld2;
  921.           bestdist = dist;
  922.           bestmdist = x2 + dx2 / 2;
  923.            }
  924.         }
  925.       }
  926.    }
  927.  
  928.    /* no intersection: the LineDef was pointing outwards! */
  929.    if (bestld < 0)
  930.       return -1;
  931.  
  932.    /* now look if this LineDef has a SideDef bound to one sector */
  933.    if (abs( dy1) > abs( dx1))
  934.    {
  935.       if ((Vertexes[ LineDefs[ bestld].start].x < Vertexes[ LineDefs[ bestld].end].x) == (dy1 > 0))
  936.      x0 = LineDefs[ bestld].sidedef1;
  937.       else
  938.      x0 = LineDefs[ bestld].sidedef2;
  939.    }
  940.    else
  941.    {
  942.       if ((Vertexes[ LineDefs[ bestld].start].y < Vertexes[ LineDefs[ bestld].end].y) != (dx1 > 0))
  943.      x0 = LineDefs[ bestld].sidedef1;
  944.       else
  945.      x0 = LineDefs[ bestld].sidedef2;
  946.    }
  947.  
  948.    /* there is no SideDef on this side of the LineDef! */
  949.    if (x0 < 0)
  950.       return -1;
  951.  
  952.    /* OK, we got it -- return the Sector number */
  953.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  954.    return SideDefs[ x0].sector;
  955. }
  956.  
  957.  
  958.  
  959. /*
  960.    copy a group of objects to a new position
  961. */
  962.  
  963. void CopyObjects( BCINT objtype, SelPtr obj) /* SWAP! */
  964. {
  965.    BCINT        n, m;
  966.    SelPtr     cur;
  967.    SelPtr     list1, list2;
  968.    SelPtr     ref1, ref2;
  969.  
  970.    if (obj == NULL)
  971.       return;
  972.    ObjectsNeeded( objtype, 0);
  973.    /* copy the object(s) */
  974.    switch (objtype)
  975.    {
  976.       case OBJ_THINGS:
  977.      for (cur = obj; cur; cur = cur->next)
  978.      {
  979.         InsertObject( OBJ_THINGS, cur->objnum, Things[ cur->objnum].xpos, Things[ cur->objnum].ypos);
  980.         cur->objnum = NumThings - 1;
  981.      }
  982.      MadeChanges = TRUE;
  983.      break;
  984.  
  985.       case OBJ_VERTEXES:
  986.      for (cur = obj; cur; cur = cur->next)
  987.      {
  988.         InsertObject( OBJ_VERTEXES, cur->objnum, Vertexes[ cur->objnum].x, Vertexes[ cur->objnum].y);
  989.         cur->objnum = NumVertexes - 1;
  990.      }
  991.      MadeChanges = TRUE;
  992.      MadeMapChanges = TRUE;
  993.      break;
  994.  
  995.       case OBJ_LINEDEFS:
  996.      list1 = NULL;
  997.      list2 = NULL;
  998.      /* create the LineDefs */
  999.      for (cur = obj; cur; cur = cur->next)
  1000.      {
  1001.         InsertObject( OBJ_LINEDEFS, cur->objnum, 0, 0);
  1002.         cur->objnum = NumLineDefs - 1;
  1003.         if (!IsSelected( list1, LineDefs[ cur->objnum].start))
  1004.         {
  1005.            SelectObject( &list1, LineDefs[ cur->objnum].start);
  1006.            SelectObject( &list2, LineDefs[ cur->objnum].start);
  1007.         }
  1008.         if (!IsSelected( list1, LineDefs[ cur->objnum].end))
  1009.         {
  1010.            SelectObject( &list1, LineDefs[ cur->objnum].end);
  1011.            SelectObject( &list2, LineDefs[ cur->objnum].end);
  1012.         }
  1013.      }
  1014.      /* create the Vertices */
  1015.      CopyObjects( OBJ_VERTEXES, list2);
  1016.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  1017.      /* update the references to the Vertexes */
  1018.      for (ref1 = list1, ref2 = list2; ref1 && ref2; ref1 = ref1->next, ref2 = ref2->next)
  1019.      {
  1020.         for (cur = obj; cur; cur = cur->next)
  1021.         {
  1022.            if (ref1->objnum == LineDefs[ cur->objnum].start)
  1023.           LineDefs[ cur->objnum].start = ref2->objnum;
  1024.            if (ref1->objnum == LineDefs[ cur->objnum].end)
  1025.           LineDefs[ cur->objnum].end = ref2->objnum;
  1026.         }
  1027.      }
  1028.      ForgetSelection( &list1);
  1029.      ForgetSelection( &list2);
  1030.      break;
  1031.  
  1032.       case OBJ_SECTORS:
  1033.      ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1034.      list1 = NULL;
  1035.      list2 = NULL;
  1036.      /* create the LineDefs (and Vertices) */
  1037.      for (cur = obj; cur; cur = cur->next)
  1038.      {
  1039.         for (n = 0; n < NumLineDefs; n++)
  1040.            if ( (((m = LineDefs[ n].sidedef1) >= 0 && SideDefs[ m].sector == cur->objnum)
  1041.           || ((m = LineDefs[ n].sidedef2) >= 0 && SideDefs[ m].sector == cur->objnum))
  1042.          && ! IsSelected( list1, n))
  1043.            {
  1044.           SelectObject( &list1, n);
  1045.           SelectObject( &list2, n);
  1046.            }
  1047.      }
  1048.      CopyObjects( OBJ_LINEDEFS, list2);
  1049.      /* create the SideDefs */
  1050.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  1051.      for (ref1 = list1, ref2 = list2; ref1 && ref2; ref1 = ref1->next, ref2 = ref2->next)
  1052.      {
  1053.         if ((n = LineDefs[ ref1->objnum].sidedef1) >= 0)
  1054.         {
  1055.            InsertObject( OBJ_SIDEDEFS, n, 0, 0);
  1056.            n = NumSideDefs - 1;
  1057.            ObjectsNeeded( OBJ_LINEDEFS, 0);
  1058.            LineDefs[ ref2->objnum].sidedef1 = n;
  1059.         }
  1060.         if ((m = LineDefs[ ref1->objnum].sidedef2) >= 0)
  1061.         {
  1062.            InsertObject( OBJ_SIDEDEFS, m, 0, 0);
  1063.            m = NumSideDefs - 1;
  1064.            ObjectsNeeded( OBJ_LINEDEFS, 0);
  1065.            LineDefs[ ref2->objnum].sidedef2 = m;
  1066.         }
  1067.         ref1->objnum = n;
  1068.         ref2->objnum = m;
  1069.      }
  1070.      /* create the Sectors */
  1071.      for (cur = obj; cur; cur = cur->next)
  1072.      {
  1073.         InsertObject( OBJ_SECTORS, cur->objnum, 0, 0);
  1074.         ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1075.         for (ref1 = list1, ref2 = list2; ref1 && ref2; ref1 = ref1->next, ref2 = ref2->next)
  1076.         {
  1077.            if (ref1->objnum >= 0 && SideDefs[ ref1->objnum].sector == cur->objnum)
  1078.           SideDefs[ ref1->objnum].sector = NumSectors - 1;
  1079.            if (ref2->objnum >= 0 && SideDefs[ ref2->objnum].sector == cur->objnum)
  1080.           SideDefs[ ref2->objnum].sector = NumSectors - 1;
  1081.         }
  1082.         cur->objnum = NumSectors - 1;
  1083.      }
  1084.      ForgetSelection( &list1);
  1085.      ForgetSelection( &list2);
  1086.      break;
  1087.    }
  1088. }
  1089.  
  1090.  
  1091.  
  1092. /*
  1093.    move a group of objects to a new position
  1094.    (must be called with obj = NULL before moving the objects)
  1095. */
  1096.  
  1097. Bool MoveObjectsToCoords( BCINT objtype, SelPtr obj, BCINT newx, BCINT newy, BCINT grid) /* SWAP! */
  1098. {
  1099.    BCINT        n, m;
  1100.    BCINT        dx, dy;
  1101.    SelPtr     cur, vertices;
  1102.    static BCINT refx, refy; /* previous position */
  1103.  
  1104.    ObjectsNeeded( objtype, 0);
  1105.    if (grid > 0)
  1106.    {
  1107.       newx = (newx + grid / 2) & ~(grid - 1);
  1108.       newy = (newy + grid / 2) & ~(grid - 1);
  1109.    }
  1110.  
  1111.    /* only update the reference point? */
  1112.    if (obj == NULL)
  1113.    {
  1114.       refx = newx;
  1115.       refy = newy;
  1116.       return TRUE;
  1117.    }
  1118.    /* compute the displacement */
  1119.    dx = newx - refx;
  1120.    dy = newy - refy;
  1121.    /* nothing to do? */
  1122.    if (dx == 0 && dy == 0)
  1123.       return FALSE;
  1124.  
  1125.    /* move the object(s) */
  1126.    switch (objtype)
  1127.    {
  1128.       case OBJ_THINGS:
  1129.      for (cur = obj; cur; cur = cur->next)
  1130.      {
  1131.         Things[ cur->objnum].xpos += dx;
  1132.         Things[ cur->objnum].ypos += dy;
  1133.      }
  1134.      refx = newx;
  1135.      refy = newy;
  1136.      MadeChanges = TRUE;
  1137.      break;
  1138.       case OBJ_VERTEXES:
  1139.      for (cur = obj; cur; cur = cur->next)
  1140.      {
  1141.         Vertexes[ cur->objnum].x += dx;
  1142.         Vertexes[ cur->objnum].y += dy;
  1143.      }
  1144.      refx = newx;
  1145.      refy = newy;
  1146.      MadeChanges = TRUE;
  1147.      MadeMapChanges = TRUE;
  1148.      break;
  1149.       case OBJ_LINEDEFS:
  1150.      vertices = NULL;
  1151.      for (cur = obj; cur; cur = cur->next)
  1152.      {
  1153.         if (!IsSelected( vertices, LineDefs[ cur->objnum].start))
  1154.            SelectObject( &vertices, LineDefs[ cur->objnum].start);
  1155.         if (!IsSelected( vertices, LineDefs[ cur->objnum].end))
  1156.            SelectObject( &vertices, LineDefs[ cur->objnum].end);
  1157.      }
  1158.      MoveObjectsToCoords( OBJ_VERTEXES, vertices, newx, newy, grid);
  1159.      ForgetSelection( &vertices);
  1160.      break;
  1161.       case OBJ_SECTORS:
  1162.      ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1163.      vertices = NULL;
  1164.      for (cur = obj; cur; cur = cur->next)
  1165.      {
  1166.         for (n = 0; n < NumLineDefs; n++)
  1167.            if (((m = LineDefs[ n].sidedef1) >= 0 && SideDefs[ m].sector == cur->objnum)
  1168.         || ((m = LineDefs[ n].sidedef2) >= 0 && SideDefs[ m].sector == cur->objnum))
  1169.            {
  1170.           if (!IsSelected( vertices, LineDefs[ n].start))
  1171.              SelectObject( &vertices, LineDefs[ n].start);
  1172.           if (!IsSelected( vertices, LineDefs[ n].end))
  1173.              SelectObject( &vertices, LineDefs[ n].end);
  1174.            }
  1175.      }
  1176.      MoveObjectsToCoords( OBJ_VERTEXES, vertices, newx, newy, grid);
  1177.      ForgetSelection( &vertices);
  1178.      break;
  1179.    }
  1180.    return TRUE;
  1181. }
  1182.  
  1183.  
  1184.  
  1185. /*
  1186.    get the coordinates (approx.) of an object
  1187. */
  1188.  
  1189. void GetObjectCoords( BCINT objtype, BCINT objnum, BCINT *xpos, BCINT *ypos) /* SWAP! */
  1190. {
  1191.    BCINT  n, v1, v2, sd1, sd2;
  1192.    long accx, accy, num;
  1193.  
  1194.    switch (objtype)
  1195.    {
  1196.       case OBJ_THINGS:
  1197.      ObjectsNeeded( OBJ_THINGS, 0);
  1198.      *xpos = Things[ objnum].xpos;
  1199.      *ypos = Things[ objnum].ypos;
  1200.      break;
  1201.       case OBJ_VERTEXES:
  1202.      ObjectsNeeded( OBJ_VERTEXES, 0);
  1203.      *xpos = Vertexes[ objnum].x;
  1204.      *ypos = Vertexes[ objnum].y;
  1205.      break;
  1206.       case OBJ_LINEDEFS:
  1207.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  1208.      v1 = LineDefs[ objnum].start;
  1209.      v2 = LineDefs[ objnum].end;
  1210.      ObjectsNeeded( OBJ_VERTEXES, 0);
  1211.      *xpos = (Vertexes[ v1].x + Vertexes[ v2].x) / 2;
  1212.      *ypos = (Vertexes[ v1].y + Vertexes[ v2].y) / 2;
  1213.      break;
  1214.       case OBJ_SIDEDEFS:
  1215.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  1216.      for (n = 0; n < NumLineDefs; n++)
  1217.         if (LineDefs[ n].sidedef1 == objnum || LineDefs[ n].sidedef2 == objnum)
  1218.         {
  1219.            v1 = LineDefs[ n].start;
  1220.            v2 = LineDefs[ n].end;
  1221.            ObjectsNeeded( OBJ_VERTEXES, 0);
  1222.            *xpos = (Vertexes[ v1].x + Vertexes[ v2].x) / 2;
  1223.            *ypos = (Vertexes[ v1].y + Vertexes[ v2].y) / 2;
  1224.            return;
  1225.         }
  1226.      *xpos = (MapMinX + MapMaxX) / 2;
  1227.      *ypos = (MapMinY + MapMaxY) / 2;
  1228.       case OBJ_SECTORS:
  1229.      accx = 0L;
  1230.      accy = 0L;
  1231.      num = 0L;
  1232.      for (n = 0; n < NumLineDefs; n++)
  1233.      {
  1234.         ObjectsNeeded( OBJ_LINEDEFS, 0);
  1235.         sd1 = LineDefs[ n].sidedef1;
  1236.         sd2 = LineDefs[ n].sidedef2;
  1237.         v1 = LineDefs[ n].start;
  1238.         v2 = LineDefs[ n].end;
  1239.         ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1240.         if ((sd1 >= 0 && SideDefs[ sd1].sector == objnum) || (sd2 >= 0 && SideDefs[ sd2].sector == objnum))
  1241.         {
  1242.            ObjectsNeeded( OBJ_VERTEXES, 0);
  1243.            /* if the Sector is closed, all Vertices will be counted twice */
  1244.            accx += (long) Vertexes[ v1].x;
  1245.            accy += (long) Vertexes[ v1].y;
  1246.            num++;
  1247.            accx += (long) Vertexes[ v2].x;
  1248.            accy += (long) Vertexes[ v2].y;
  1249.            num++;
  1250.         }
  1251.      }
  1252.      if (num > 0)
  1253.      {
  1254.         *xpos = (BCINT) ((accx + num / 2L) / num);
  1255.         *ypos = (BCINT) ((accy + num / 2L) / num);
  1256.      }
  1257.      else
  1258.      {
  1259.         *xpos = (MapMinX + MapMaxX) / 2;
  1260.         *ypos = (MapMinY + MapMaxY) / 2;
  1261.      }
  1262.      break;
  1263.    }
  1264. }
  1265.  
  1266.  
  1267.  
  1268. /*
  1269.    rotate and scale a group of objects around the center of gravity
  1270. */
  1271.  
  1272. void RotateAndScaleObjects( BCINT objtype, SelPtr obj, double angle, double scale) /* SWAP! */
  1273. {
  1274.    BCINT    n, m;
  1275.    BCINT    dx, dy;
  1276.    BCINT    centerx, centery;
  1277.    long   accx, accy, num;
  1278.    SelPtr cur, vertices;
  1279.  
  1280.    if (obj == NULL)
  1281.       return;
  1282.    ObjectsNeeded( objtype, 0);
  1283.  
  1284.    switch (objtype)
  1285.    {
  1286.       case OBJ_THINGS:
  1287.      accx = 0L;
  1288.      accy = 0L;
  1289.      num = 0L;
  1290.      for (cur = obj; cur; cur = cur->next)
  1291.      {
  1292.         accx += (long) Things[ cur->objnum].xpos;
  1293.         accy += (long) Things[ cur->objnum].ypos;
  1294.         num++;
  1295.      }
  1296.      centerx = (BCINT) ((accx + num / 2L) / num);
  1297.      centery = (BCINT) ((accy + num / 2L) / num);
  1298.      for (cur = obj; cur; cur = cur->next)
  1299.      {
  1300.         dx = Things[ cur->objnum].xpos - centerx;
  1301.         dy = Things[ cur->objnum].ypos - centery;
  1302.         RotateAndScaleCoords( &dx, &dy, angle, scale);
  1303.         Things[ cur->objnum].xpos = centerx + dx;
  1304.         Things[ cur->objnum].ypos = centery + dy;
  1305.      }
  1306.      MadeChanges = TRUE;
  1307.      break;
  1308.       case OBJ_VERTEXES:
  1309.      accx = 0L;
  1310.      accy = 0L;
  1311.      num = 0L;
  1312.      for (cur = obj; cur; cur = cur->next)
  1313.      {
  1314.         accx += (long) Vertexes[ cur->objnum].x;
  1315.         accy += (long) Vertexes[ cur->objnum].y;
  1316.         num++;
  1317.      }
  1318.      centerx = (BCINT) ((accx + num / 2L) / num);
  1319.      centery = (BCINT) ((accy + num / 2L) / num);
  1320.      for (cur = obj; cur; cur = cur->next)
  1321.      {
  1322.         dx = Vertexes[ cur->objnum].x - centerx;
  1323.         dy = Vertexes[ cur->objnum].y - centery;
  1324.         RotateAndScaleCoords( &dx, &dy, angle, scale);
  1325.         Vertexes[ cur->objnum].x = (centerx + dx + 4) & ~7;
  1326.         Vertexes[ cur->objnum].y = (centery + dy + 4) & ~7;
  1327.      }
  1328.      MadeChanges = TRUE;
  1329.      MadeMapChanges = TRUE;
  1330.      break;
  1331.       case OBJ_LINEDEFS:
  1332.      vertices = NULL;
  1333.      for (cur = obj; cur; cur = cur->next)
  1334.      {
  1335.         if (!IsSelected( vertices, LineDefs[ cur->objnum].start))
  1336.            SelectObject( &vertices, LineDefs[ cur->objnum].start);
  1337.         if (!IsSelected( vertices, LineDefs[ cur->objnum].end))
  1338.            SelectObject( &vertices, LineDefs[ cur->objnum].end);
  1339.      }
  1340.      RotateAndScaleObjects( OBJ_VERTEXES, vertices, angle, scale);
  1341.      ForgetSelection( &vertices);
  1342.      break;
  1343.       case OBJ_SECTORS:
  1344.      ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1345.      vertices = NULL;
  1346.      for (cur = obj; cur; cur = cur->next)
  1347.      {
  1348.         for (n = 0; n < NumLineDefs; n++)
  1349.            if (((m = LineDefs[ n].sidedef1) >= 0 && SideDefs[ m].sector == cur->objnum)
  1350.         || ((m = LineDefs[ n].sidedef2) >= 0 && SideDefs[ m].sector == cur->objnum))
  1351.            {
  1352.           if (!IsSelected( vertices, LineDefs[ n].start))
  1353.              SelectObject( &vertices, LineDefs[ n].start);
  1354.           if (!IsSelected( vertices, LineDefs[ n].end))
  1355.              SelectObject( &vertices, LineDefs[ n].end);
  1356.            }
  1357.      }
  1358.      RotateAndScaleObjects( OBJ_VERTEXES, vertices, angle, scale);
  1359.      ForgetSelection( &vertices);
  1360.      break;
  1361.    }
  1362. }
  1363.  
  1364.  
  1365.  
  1366. /*
  1367.    find a free tag number
  1368. */
  1369.  
  1370. BCINT FindFreeTag() /* SWAP! */
  1371. {
  1372.    BCINT  tag, n;
  1373.    Bool ok;
  1374.  
  1375.    ObjectsNeeded( OBJ_LINEDEFS, OBJ_SECTORS, 0);
  1376.    tag = 1;
  1377.    ok = FALSE;
  1378.    while (! ok)
  1379.    {
  1380.       ok = TRUE;
  1381.       for (n = 0; n < NumLineDefs; n++)
  1382.      if (LineDefs[ n].tag == tag)
  1383.      {
  1384.         ok = FALSE;
  1385.         break;
  1386.      }
  1387.       if (ok)
  1388.      for (n = 0; n < NumSectors; n++)
  1389.         if (Sectors[ n].tag == tag)
  1390.         {
  1391.            ok = FALSE;
  1392.            break;
  1393.         }
  1394.       tag++;
  1395.    }
  1396.    return tag - 1;
  1397. }
  1398.  
  1399.  
  1400.  
  1401. /*
  1402.    flip one or several LineDefs
  1403. */
  1404.  
  1405. void FlipLineDefs( SelPtr obj, Bool swapvertices) /* SWAP! */
  1406. {
  1407.    SelPtr cur;
  1408.    BCINT    tmp;
  1409.  
  1410.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  1411.    for (cur = obj; cur; cur = cur->next)
  1412.    {
  1413.       if (swapvertices)
  1414.       {
  1415.      /* swap starting and ending Vertices */
  1416.      tmp = LineDefs[ cur->objnum].end;
  1417.      LineDefs[ cur->objnum].end = LineDefs[ cur->objnum].start;
  1418.      LineDefs[ cur->objnum].start = tmp;
  1419.       }
  1420.       /* swap first and second SideDefs */
  1421.       tmp = LineDefs[ cur->objnum].sidedef1;
  1422.       LineDefs[ cur->objnum].sidedef1 = LineDefs[ cur->objnum].sidedef2;
  1423.       LineDefs[ cur->objnum].sidedef2 = tmp;
  1424.    }
  1425.    MadeChanges = TRUE;
  1426.    MadeMapChanges = TRUE;
  1427. }
  1428.  
  1429.  
  1430.  
  1431. /*
  1432.    delete a Vertex and join the two Linedefs
  1433. */
  1434.  
  1435. void DeleteVerticesJoinLineDefs( SelPtr obj) /* SWAP! */
  1436. {
  1437.    BCINT    lstart, lend, l;
  1438.    SelPtr cur;
  1439.    char   msg[ 80];
  1440.  
  1441.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  1442.    while (obj != NULL)
  1443.    {
  1444.       cur = obj;
  1445.       obj = obj->next;
  1446.       lstart = -1;
  1447.       lend = -1;
  1448.       for (l = 0; l < NumLineDefs; l++)
  1449.       {
  1450.      if (LineDefs[ l].start == cur->objnum)
  1451.      {
  1452.         if (lstart == -1)
  1453.            lstart = l;
  1454.         else
  1455.            lstart = -2;
  1456.      }
  1457.      if (LineDefs[ l].end == cur->objnum)
  1458.      {
  1459.         if (lend == -1)
  1460.            lend = l;
  1461.         else
  1462.            lend = -2;
  1463.      }
  1464.       }
  1465.       if (lstart < 0 || lend < 0)
  1466.       {
  1467.      Beep();
  1468.      sprintf(msg, "Cannot delete Vertex #%d and join the LineDefs", cur->objnum);
  1469.      Notify( -1, -1, msg, "The Vertex must be the start of one LineDef and the end of another one");
  1470.      continue;
  1471.       }
  1472.       LineDefs[ lend].end = LineDefs[ lstart].end;
  1473.       DeleteObject( OBJ_LINEDEFS, lstart);
  1474.       DeleteObject( OBJ_VERTEXES, cur->objnum);
  1475.       MadeChanges = TRUE;
  1476.       MadeMapChanges = TRUE;
  1477.    }
  1478. }
  1479.  
  1480.  
  1481.  
  1482. /*
  1483.    merge several vertices into one
  1484. */
  1485.  
  1486. void MergeVertices( SelPtr *list) /* SWAP! */
  1487. {
  1488.    BCINT    v, l;
  1489.  
  1490.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  1491.    v = (*list)->objnum;
  1492.    UnSelectObject( list, v);
  1493.    if (*list == NULL)
  1494.    {
  1495.       Beep();
  1496.       Notify( -1, -1, "You must select at least two vertices", NULL);
  1497.       return;
  1498.    }
  1499.    /* change the LineDefs starts & ends */
  1500.    for (l = 0; l < NumLineDefs; l++)
  1501.    {
  1502.       if (IsSelected( *list, LineDefs[ l].start))
  1503.       {      
  1504.      /* don't change a LineDef that has both ends on the same spot */
  1505.      if (!IsSelected( *list, LineDefs[ l].end) && LineDefs[ l].end != v)
  1506.         LineDefs[ l].start = v;
  1507.       }
  1508.       else if (IsSelected( *list, LineDefs[ l].end))
  1509.       {
  1510.      /* idem */
  1511.      if (LineDefs[ l].start != v)
  1512.         LineDefs[ l].end = v;
  1513.       }
  1514.    }
  1515.    /* delete the Vertices (and some LineDefs too) */
  1516.    DeleteObjects( OBJ_VERTEXES, list);
  1517.    MadeChanges = TRUE;
  1518.    MadeMapChanges = TRUE;
  1519. }
  1520.  
  1521.  
  1522.  
  1523. /*
  1524.    check if some vertices should be merged into one
  1525. */
  1526.  
  1527. Bool AutoMergeVertices( SelPtr *list) /* SWAP! */
  1528. {
  1529.    SelPtr ref, cur;
  1530.    Bool   confirmed, redraw, flipped, mergedone, isldend;
  1531.    BCINT  v, refv, ld, sd, oldnumld;
  1532.  
  1533.    ObjectsNeeded( OBJ_VERTEXES, 0);
  1534.    confirmed = FALSE;
  1535.    redraw = FALSE;
  1536.    mergedone = FALSE;
  1537.    isldend = FALSE;
  1538.    ref = *list;
  1539.    while (ref)
  1540.    {
  1541.       refv = ref->objnum;
  1542.       ref = ref->next;
  1543.       /* check if there is a Vertex at the same position (same X and Y) */
  1544.       for (v = 0; v < NumVertexes; v++)
  1545.      if (v != refv && Vertexes[ refv].x == Vertexes[ v].x && Vertexes[ refv].y == Vertexes[ v].y)
  1546.      {
  1547.         redraw = TRUE;
  1548.         if (confirmed || Expert || Confirm( -1, -1, "Some Vertices occupy the same position", "Do you want to merge them into one?"))
  1549.         {
  1550.            /* don't ask for confirmation twice */
  1551.            confirmed = TRUE;
  1552.            /* merge the two vertices */
  1553.             mergedone = TRUE;
  1554.            cur = NULL;
  1555.            SelectObject( &cur, refv);
  1556.            SelectObject( &cur, v);
  1557.            MergeVertices( &cur);
  1558.            /* not useful but safer... */
  1559.            ObjectsNeeded( OBJ_VERTEXES, 0);
  1560.            /* update the references in the selection list */
  1561.            for (cur = *list; cur; cur = cur->next)
  1562.           if (cur->objnum > refv)
  1563.              cur->objnum = cur->objnum - 1;
  1564.            if (v > refv)
  1565.           v--;
  1566.            /* the old Vertex has been deleted */
  1567.            UnSelectObject( list, refv);
  1568.            /* select the new Vertex instead */
  1569.            if (!IsSelected( *list, v))
  1570.           SelectObject( list, v);
  1571.            break;
  1572.         }
  1573.         else
  1574.            return redraw;
  1575.      }
  1576.    }
  1577.    confirmed = FALSE;
  1578.    ref = *list;
  1579.    while (ref)
  1580.    {
  1581.       refv = ref->objnum;
  1582.       ref = ref->next;
  1583.       oldnumld = NumLineDefs;
  1584.       /* check if this Vertex is on a LineDef */
  1585.       for (ld = 0; ld < oldnumld; ld++)
  1586.       {
  1587.      ObjectsNeeded( OBJ_VERTEXES, OBJ_LINEDEFS, 0);
  1588.      if (LineDefs[ ld].start == refv || LineDefs[ ld].end == refv)
  1589.          {
  1590.           /* one Vertex had a LineDef bound to it -- check it later */
  1591.             isldend = TRUE;
  1592.          }
  1593.          else if (IsLineDefInside( ld, Vertexes[ refv].x - 3, Vertexes[ refv].y - 3, Vertexes[ refv].x + 3, Vertexes[ refv].y + 3))
  1594.      {
  1595.         redraw = TRUE;
  1596.         if (confirmed || Expert || Confirm( -1, -1, "Some Vertices are on a LineDef", "Do you want to split the LineDef there?"))
  1597.         {
  1598.            /* don't ask for confirmation twice */
  1599.            confirmed = TRUE;
  1600.            /* split the LineDef */
  1601.             mergedone = TRUE;
  1602.            InsertObject( OBJ_LINEDEFS, ld, 0, 0);
  1603.            LineDefs[ ld].end = refv;
  1604.            LineDefs[ NumLineDefs - 1].start = refv;
  1605.            sd = LineDefs[ ld].sidedef1;
  1606.            if (sd >= 0)
  1607.            {
  1608.           InsertObject( OBJ_SIDEDEFS, sd, 0, 0);
  1609.           ObjectsNeeded( OBJ_LINEDEFS, 0);
  1610.           LineDefs[ NumLineDefs - 1].sidedef1 = NumSideDefs - 1;
  1611.            }
  1612.            sd = LineDefs[ ld].sidedef2;
  1613.            if (sd >= 0)
  1614.            {
  1615.                InsertObject( OBJ_SIDEDEFS, sd, 0, 0);
  1616.                ObjectsNeeded( OBJ_LINEDEFS, 0);
  1617.                LineDefs[ NumLineDefs - 1].sidedef2 = NumSideDefs - 1;
  1618.            }
  1619.            MadeChanges = TRUE;
  1620.            MadeMapChanges = TRUE;
  1621.         }
  1622.         else
  1623.            return redraw;
  1624.      }
  1625.       }
  1626.    }
  1627.    /* don't continue if this isn't necessary */
  1628.    if (isldend == FALSE || mergedone == FALSE)
  1629.       return redraw;
  1630.  
  1631.    confirmed = FALSE;
  1632.    /* test if two LineDefs are at between the same pair of Vertices */
  1633.    for (v = 0; v < NumLineDefs - 1; v++)
  1634.       for (ld = v + 1; ld < NumLineDefs; ld++)
  1635.      if ((LineDefs[ v].start == LineDefs[ ld].start && LineDefs[ v].end == LineDefs[ ld].end)
  1636.       || (LineDefs[ v].start == LineDefs[ ld].end && LineDefs[ v].end == LineDefs[ ld].start))
  1637.      {
  1638.         redraw = TRUE;
  1639.         if (confirmed || Expert || Confirm( -1, -1, "Some LineDefs are superimposed", "Do you want to merge them into one?"))
  1640.         {
  1641.            /* don't ask for confirmation twice */
  1642.            confirmed = TRUE;
  1643.            /* test if the LineDefs have the same orientation */
  1644.            if (LineDefs[ v].start == LineDefs[ ld].end)
  1645.           flipped = TRUE;
  1646.            else
  1647.           flipped = FALSE;
  1648.            /* merge the two LineDefs */
  1649.            if (LineDefs[ v].sidedef1 < 0)
  1650.            {
  1651.                 if (flipped)
  1652.           {
  1653.             LineDefs[ v].sidedef1 = LineDefs[ ld].sidedef2;
  1654.             LineDefs[ ld].sidedef2 = -1;
  1655.           }
  1656.           else
  1657.           {
  1658.             LineDefs[ v].sidedef1 = LineDefs[ ld].sidedef1;
  1659.             LineDefs[ ld].sidedef1 = -1;
  1660.           }
  1661.            }
  1662.            if (LineDefs[ v].sidedef2 < 0)
  1663.            {
  1664.           if (flipped)
  1665.           {
  1666.             LineDefs[ v].sidedef2 = LineDefs[ ld].sidedef1;
  1667.             LineDefs[ ld].sidedef1 = -1;
  1668.           }
  1669.           else
  1670.           {
  1671.                   LineDefs[ v].sidedef2 = LineDefs[ ld].sidedef2;
  1672.                   LineDefs[ ld].sidedef2 = -1;
  1673.                 }
  1674.                  }
  1675.                  if (LineDefs[ v].sidedef1 >= 0 && LineDefs[ v].sidedef2 >= 0 && (LineDefs[ v].flags & 0x04) == 0)
  1676.                 LineDefs[ v].flags = 0x04;
  1677.                  DeleteObject( OBJ_LINEDEFS, ld);
  1678.               }
  1679.            }
  1680.     return redraw;
  1681. }
  1682.  
  1683.  
  1684.  
  1685. /*
  1686.    split one or more LineDefs in two, adding new Vertices in the middle
  1687. */
  1688.  
  1689. void SplitLineDefs( SelPtr obj) /* SWAP! */
  1690. {
  1691.    SelPtr cur;
  1692.    BCINT    vstart, vend, sd;
  1693.  
  1694.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  1695.    for (cur = obj; cur; cur = cur->next)
  1696.    {
  1697.       vstart = LineDefs[ cur->objnum].start;
  1698.       vend = LineDefs[ cur->objnum].end;
  1699.       InsertObject( OBJ_VERTEXES, -1, (Vertexes[ vstart].x + Vertexes[ vend].x) / 2, (Vertexes[ vstart].y + Vertexes[ vend].y) / 2);
  1700.       InsertObject( OBJ_LINEDEFS, cur->objnum, 0, 0);
  1701.       LineDefs[ cur->objnum].end = NumVertexes - 1;
  1702.       LineDefs[ NumLineDefs - 1].start = NumVertexes - 1;
  1703.       sd = LineDefs[ cur->objnum].sidedef1;
  1704.       if (sd >= 0)
  1705.       {
  1706.      InsertObject( OBJ_SIDEDEFS, sd, 0, 0);
  1707.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  1708.      LineDefs[ NumLineDefs - 1].sidedef1 = NumSideDefs - 1;
  1709.       }
  1710.       sd = LineDefs[ cur->objnum].sidedef2;
  1711.       if (sd >= 0)
  1712.       {
  1713.      InsertObject( OBJ_SIDEDEFS, sd, 0, 0);
  1714.      ObjectsNeeded( OBJ_LINEDEFS, 0);
  1715.      LineDefs[ NumLineDefs - 1].sidedef2 = NumSideDefs - 1;
  1716.       }                
  1717.    }
  1718.    MadeChanges = TRUE;
  1719.    MadeMapChanges = TRUE;
  1720. }
  1721.  
  1722.  
  1723.  
  1724. /*
  1725.    split a Sector in two, adding a new LineDef between the two Vertices
  1726. */
  1727.  
  1728. void SplitSector( BCINT vertex1, BCINT vertex2) /* SWAP! */
  1729. {
  1730.    SelPtr llist;
  1731.    BCINT    curv, s, l, sd;
  1732.    char   msg1[ 80], msg2[ 80];
  1733.  
  1734.    /* check if there is a Sector between the two Vertices (in the middle) */
  1735.    s = GetCurObject( OBJ_SECTORS, Vertexes[ vertex1].x, Vertexes[ vertex1].y, Vertexes[ vertex2].x, Vertexes[ vertex2].y);
  1736.    if (s < 0)
  1737.    {
  1738.       Beep();
  1739.       sprintf( msg1, "There is no Sector between Vertex #%d and Vertex #%d", vertex1, vertex2);
  1740.       Notify( -1, -1, msg1, NULL);
  1741.       return;
  1742.    }
  1743.    /* check if there is a closed path from vertex1 to vertex2, along the edge of the Sector s */
  1744.    ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1745.    llist = NULL;
  1746.    curv = vertex1;
  1747.    while (curv != vertex2)
  1748.    {
  1749.       for (l = 0; l < NumLineDefs; l++)
  1750.       {
  1751.      sd = LineDefs[ l].sidedef1;
  1752.      if (sd >= 0 && SideDefs[ sd].sector == s && LineDefs[ l].start == curv)
  1753.      {
  1754.         curv = LineDefs[ l].end;
  1755.         SelectObject( &llist, l);
  1756.         break;
  1757.      }
  1758.      sd = LineDefs[ l].sidedef2;
  1759.      if (sd >= 0 && SideDefs[ sd].sector == s && LineDefs[ l].end == curv)
  1760.      {
  1761.         curv = LineDefs[ l].start;
  1762.         SelectObject( &llist, l);
  1763.         break;
  1764.      }
  1765.       }
  1766.       if (l >= NumLineDefs)
  1767.       {
  1768.      Beep();
  1769.      sprintf( msg1, "Cannot find a closed path from Vertex #%d to Vertex #%d", vertex1, vertex2);
  1770.      if (curv == vertex1)
  1771.         sprintf( msg2, "There is no SideDef starting from Vertex #%d on Sector #%d", vertex1, s);
  1772.      else
  1773.         sprintf( msg2, "Check if Sector #%d is closed (cannot go past Vertex #%d)", s, curv);
  1774.      Notify( -1, -1, msg1, msg2);
  1775.      ForgetSelection( &llist);
  1776.      return;
  1777.       }
  1778.       if (curv == vertex1)
  1779.       {
  1780.      Beep();    
  1781.      sprintf( msg1, "Vertex #%d is not on the same Sector (#%d) as Vertex #%d", vertex2, s, vertex1);
  1782.      Notify( -1, -1, msg1, NULL);
  1783.      ForgetSelection( &llist);
  1784.      return;
  1785.       }
  1786.    }
  1787.    /* now, the list of LineDefs for the new Sector is in llist */
  1788.  
  1789.    /* add the new Sector, LineDef and SideDefs */
  1790.    InsertObject( OBJ_SECTORS, s, 0, 0);
  1791.    InsertObject( OBJ_LINEDEFS, -1, 0, 0);
  1792.    LineDefs[ NumLineDefs - 1].start = vertex1;
  1793.    LineDefs[ NumLineDefs - 1].end = vertex2;
  1794.    LineDefs[ NumLineDefs - 1].flags = 4;
  1795.    InsertObject( OBJ_SIDEDEFS, -1, 0, 0);
  1796.    SideDefs[ NumSideDefs - 1].sector = s;
  1797.    strncpy( SideDefs[ NumSideDefs - 1].tex3, "-", 8);
  1798.    InsertObject( OBJ_SIDEDEFS, -1, 0, 0);
  1799.    strncpy( SideDefs[ NumSideDefs - 1].tex3, "-", 8);
  1800.    ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1801.    LineDefs[ NumLineDefs - 1].sidedef1 = NumSideDefs - 2;
  1802.    LineDefs[ NumLineDefs - 1].sidedef2 = NumSideDefs - 1;
  1803.  
  1804.    /* bind all LineDefs in llist to the new Sector */
  1805.    while (llist)
  1806.    {
  1807.       sd = LineDefs[ llist->objnum].sidedef1;
  1808.       if (sd < 0 || SideDefs[ sd].sector != s)
  1809.      sd = LineDefs[ llist->objnum].sidedef2;
  1810.       SideDefs[ sd].sector = NumSectors - 1;
  1811.       UnSelectObject( &llist, llist->objnum);
  1812.    }
  1813.  
  1814.  
  1815.    /* second check... uselful for Sectors within Sectors */
  1816.    ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1817.    for (l = 0; l < NumLineDefs; l++)
  1818.    {
  1819.       sd = LineDefs[ l].sidedef1;
  1820.       if (sd >= 0 && SideDefs[ sd].sector == s)
  1821.       {
  1822.          curv = GetOppositeSector( l, TRUE);
  1823.          ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1824.          if (curv == NumSectors - 1)
  1825.             SideDefs[ sd].sector = NumSectors - 1;
  1826.       }
  1827.       sd = LineDefs[ l].sidedef2;
  1828.       if (sd >= 0 && SideDefs[ sd].sector == s)
  1829.       {
  1830.          curv = GetOppositeSector( l, FALSE);
  1831.          ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1832.          if (curv == NumSectors - 1)
  1833.             SideDefs[ sd].sector = NumSectors - 1;
  1834.       }
  1835.    }
  1836.  
  1837.    MadeChanges = TRUE;
  1838.    MadeMapChanges = TRUE;
  1839. }
  1840.  
  1841.  
  1842.  
  1843. /*
  1844.    split two LineDefs, then split the Sector and add a new LineDef between the new Vertices
  1845. */
  1846.  
  1847. void SplitLineDefsAndSector( BCINT linedef1, BCINT linedef2) /* SWAP! */
  1848. {
  1849.    SelPtr llist;
  1850.    BCINT    s1, s2, s3, s4;
  1851.    char   msg[ 80];
  1852.  
  1853.    /* check if the two LineDefs are adjacent to the same Sector */
  1854.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  1855.    s1 = LineDefs[ linedef1].sidedef1;
  1856.    s2 = LineDefs[ linedef1].sidedef2;
  1857.    s3 = LineDefs[ linedef2].sidedef1;
  1858.    s4 = LineDefs[ linedef2].sidedef2;
  1859.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1860.    if (s1 >= 0)
  1861.       s1 = SideDefs[ s1].sector;
  1862.    if (s2 >= 0)
  1863.       s2 = SideDefs[ s2].sector;
  1864.    if (s3 >= 0)
  1865.       s3 = SideDefs[ s3].sector;
  1866.    if (s4 >= 0)
  1867.       s4 = SideDefs[ s4].sector;
  1868.    if ((s1 < 0 || (s1 != s3 && s1 != s4)) && (s2 < 0 || (s2 != s3 && s2 != s4)))
  1869.    {
  1870.       Beep();
  1871.       sprintf( msg, "LineDefs #%d and #%d are not adjacent to the same Sector", linedef1, linedef2);
  1872.       Notify( -1, -1, msg, NULL);
  1873.       return;
  1874.    }
  1875.    /* split the two LineDefs and create two new Vertices */
  1876.    llist = NULL;
  1877.    SelectObject( &llist, linedef1);
  1878.    SelectObject( &llist, linedef2);
  1879.    SplitLineDefs( llist);
  1880.    ForgetSelection( &llist);
  1881.    /* split the Sector and create a LineDef between the two Vertices */
  1882.    SplitSector( NumVertexes - 1, NumVertexes - 2);
  1883. }
  1884.  
  1885.            
  1886. /*
  1887.    merge two or more Sectors into one
  1888. */
  1889.  
  1890. void MergeSectors( SelPtr *slist) /* SWAP! */
  1891. {
  1892.   SelPtr cur;
  1893.   BCINT    n, olds, news;
  1894.  
  1895.   /* save the first Sector number */
  1896.   news = (*slist)->objnum;
  1897.   UnSelectObject( slist, news);
  1898.   ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1899.  
  1900.   /* change all SideDefs references to the other Sectors */
  1901.   for (cur = *slist; cur; cur = cur->next)
  1902.   {
  1903.      olds = cur->objnum;
  1904.      for (n = 0; n < NumSideDefs; n++)
  1905.      {
  1906.     if (SideDefs[ n].sector == olds)
  1907.        SideDefs[ n].sector = news;
  1908.      }
  1909.   }
  1910.  
  1911.   /* delete the Sectors */
  1912.   DeleteObjects( OBJ_SECTORS, slist);
  1913.  
  1914.   /* the returned list contains only the first Sector */
  1915.   SelectObject( slist, news);
  1916. }
  1917.  
  1918.  
  1919.  
  1920. /*
  1921.    delete one or several two-sided LineDefs and join the two Sectors
  1922. */
  1923.  
  1924. void DeleteLineDefsJoinSectors( SelPtr *ldlist) /* SWAP! */
  1925. {
  1926.    SelPtr cur, slist;
  1927.    BCINT  sd1, sd2, s1, s2;
  1928.    char   msg[ 80];
  1929.  
  1930.    /* first, do the tests for all LineDefs */
  1931.    for (cur = *ldlist; cur; cur = cur->next)
  1932.    {
  1933.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  1934.       sd1 = LineDefs[ cur->objnum].sidedef1;
  1935.       sd2 = LineDefs[ cur->objnum].sidedef2;
  1936.       if (sd1 < 0 || sd2 < 0)
  1937.       {
  1938.          Beep();
  1939.          sprintf( msg, "ERROR: LineDef #%d has only one side", cur->objnum);
  1940.          Notify( -1, -1, msg, NULL);
  1941.          return;
  1942.       }
  1943.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1944.       s1 = SideDefs[ sd1].sector;
  1945.       s2 = SideDefs[ sd2].sector;
  1946.       if (s1 < 0 || s2 < 0)
  1947.       {
  1948.          Beep();
  1949.          sprintf( msg, "ERROR: LineDef #%d has two sides, but one", cur->objnum);
  1950.          Notify( -1, -1, msg, "side is not bound to any Sector");
  1951.          return;
  1952.       }
  1953.    }
  1954.  
  1955.    /* then join the Sectors and delete the LineDefs */
  1956.    for (cur = *ldlist; cur; cur = cur->next)
  1957.    {
  1958.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  1959.       sd1 = LineDefs[ cur->objnum].sidedef1;
  1960.       sd2 = LineDefs[ cur->objnum].sidedef2;
  1961.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1962.       s1 = SideDefs[ sd1].sector;
  1963.       s2 = SideDefs[ sd2].sector;
  1964.       slist = NULL;
  1965.       SelectObject( &slist, s2);
  1966.       SelectObject( &slist, s1);
  1967.       MergeSectors( &slist);
  1968.       ForgetSelection( &slist);
  1969.    }
  1970.    DeleteObjects( OBJ_LINEDEFS, ldlist);
  1971. }
  1972.  
  1973.  
  1974.  
  1975.  
  1976. /*
  1977.    turn a Sector into a door: change the LineDefs and SideDefs
  1978. */
  1979.  
  1980. void MakeDoorFromSector( BCINT sector) /* SWAP! */
  1981. {
  1982.    BCINT    sd1, sd2;
  1983.    BCINT    n, s;
  1984.    SelPtr ldok, ldflip, ld1s;
  1985.  
  1986.    ldok = NULL;
  1987.    ldflip = NULL;
  1988.    ld1s = NULL;
  1989.    s = 0;
  1990.    /* build lists of LineDefs that border the Sector */
  1991.    for (n = 0; n < NumLineDefs; n++)
  1992.    {
  1993.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  1994.       sd1 = LineDefs[ n].sidedef1;
  1995.       sd2 = LineDefs[ n].sidedef2;
  1996.       if (sd1 >= 0 && sd2 >= 0)
  1997.       {
  1998.      ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1999.      if (SideDefs[ sd2].sector == sector)
  2000.      {
  2001.         SelectObject( &ldok, n); /* already ok */
  2002.         s++;
  2003.      }
  2004.      if (SideDefs[ sd1].sector == sector)
  2005.      {
  2006.         SelectObject( &ldflip, n); /* must be flipped */
  2007.         s++;
  2008.      }
  2009.       }
  2010.       else if (sd1 >= 0 && sd2 < 0)
  2011.       {
  2012.      ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2013.      if (SideDefs[ sd1].sector == sector)
  2014.         SelectObject( &ld1s, n); /* wall (one-sided) */
  2015.       }
  2016.    }
  2017.    /* a normal door has two sides... */
  2018.    if (s < 2)
  2019.    {
  2020.       Beep();
  2021.       Notify( -1, -1, "The door must be connected to two other Sectors.", NULL);
  2022.       ForgetSelection( &ldok);
  2023.       ForgetSelection( &ldflip);
  2024.       ForgetSelection( &ld1s);
  2025.       return;
  2026.    }
  2027.    if ((s > 2) && !(Expert || Confirm( -1, -1, "The door will have more than two sides.", "Do you still want to create it?")))
  2028.    {
  2029.       ForgetSelection( &ldok);
  2030.       ForgetSelection( &ldflip);
  2031.       ForgetSelection( &ld1s);
  2032.       return;
  2033.    }
  2034.    /* flip the LineDefs that have the wrong orientation */
  2035.    if (ldflip != NULL)
  2036.       FlipLineDefs( ldflip, TRUE);
  2037.    /* merge the two selection lists */
  2038.    while (ldflip != NULL)
  2039.    {
  2040.       if (!IsSelected( ldok, ldflip->objnum))
  2041.      SelectObject( &ldok, ldflip->objnum);
  2042.       UnSelectObject( &ldflip, ldflip->objnum);
  2043.    }
  2044.    /* change the LineDefs and SideDefs */
  2045.    while (ldok != NULL)
  2046.    {
  2047.       /* give the "normal door" type and flags to the LineDef */
  2048.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2049.       n = ldok->objnum;
  2050.       LineDefs[ n].type = 1;
  2051.       LineDefs[ n].flags = 0x04;
  2052.       sd1 = LineDefs[ n].sidedef1;
  2053.       sd2 = LineDefs[ n].sidedef2;
  2054.       /* adjust the textures for the SideDefs */
  2055.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2056.       if (strncmp( SideDefs[ sd1].tex3, "-", 8))
  2057.       {
  2058.      if (!strncmp( SideDefs[ sd1].tex1, "-", 8))
  2059.         strncpy( SideDefs[ sd1].tex1, SideDefs[ sd1].tex3, 8);
  2060.      strncpy( SideDefs[ sd1].tex3, "-", 8);
  2061.       }
  2062.       if (!strncmp( SideDefs[ sd1].tex1, "-", 8))
  2063.      strncpy( SideDefs[ sd1].tex1, "BIGDOOR2", 8);
  2064.       strncpy( SideDefs[ sd2].tex3, "-", 8);
  2065.       UnSelectObject( &ldok, n);
  2066.    }
  2067.    while (ld1s != NULL)
  2068.    {
  2069.       /* give the "door side" flags to the LineDef */
  2070.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2071.       n = ld1s->objnum;
  2072.       LineDefs[ n].flags = 0x11;
  2073.       sd1 = LineDefs[ n].sidedef1;
  2074.       /* adjust the textures for the SideDef */
  2075.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2076.       if (!strncmp( SideDefs[ sd1].tex3, "-", 8))
  2077.      strncpy( SideDefs[ sd1].tex3, "DOORTRAK", 8);
  2078.       strncpy( SideDefs[ sd1].tex1, "-", 8);
  2079.       strncpy( SideDefs[ sd1].tex2, "-", 8);
  2080.       UnSelectObject( &ld1s, n);
  2081.    }
  2082.    /* adjust the ceiling height */
  2083.    ObjectsNeeded( OBJ_SECTORS, 0);
  2084.    Sectors[ sector].ceilh = Sectors[ sector].floorh;
  2085. }
  2086.  
  2087.  
  2088.  
  2089. /*
  2090.    turn a Sector into a lift: change the LineDefs and SideDefs
  2091. */
  2092.  
  2093. void MakeLiftFromSector( BCINT sector) /* SWAP! */
  2094. {
  2095.    BCINT    sd1, sd2;
  2096.    BCINT    n, s, tag;
  2097.    SelPtr ldok, ldflip, ld1s;
  2098.    SelPtr sect, curs;
  2099.    BCINT    minh, maxh;
  2100.  
  2101.    ldok = NULL;
  2102.    ldflip = NULL;
  2103.    ld1s = NULL;
  2104.    sect = NULL;
  2105.    /* build lists of LineDefs that border the Sector */
  2106.    for (n = 0; n < NumLineDefs; n++)
  2107.    {
  2108.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2109.       sd1 = LineDefs[ n].sidedef1;
  2110.       sd2 = LineDefs[ n].sidedef2;
  2111.       if (sd1 >= 0 && sd2 >= 0)
  2112.       {
  2113.      ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2114.      if (SideDefs[ sd2].sector == sector)
  2115.      {
  2116.         SelectObject( &ldok, n); /* already ok */
  2117.         s = SideDefs[ sd1].sector;
  2118.         if (s != sector && !IsSelected( sect, s))
  2119.            SelectObject( §, s);
  2120.      }
  2121.      if (SideDefs[ sd1].sector == sector)
  2122.      {
  2123.         SelectObject( &ldflip, n); /* will be flipped */
  2124.         s = SideDefs[ sd2].sector;
  2125.         if (s != sector && !IsSelected( sect, s))
  2126.            SelectObject( §, s);
  2127.      }
  2128.       }
  2129.       else if (sd1 >= 0 && sd2 < 0)
  2130.       {
  2131.      ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2132.      if (SideDefs[ sd1].sector == sector)
  2133.         SelectObject( &ld1s, n); /* wall (one-sided) */
  2134.       }
  2135.    }
  2136.    /* there must be a way to go on the lift... */
  2137.    if (sect == NULL)
  2138.    {
  2139.       Beep();
  2140.       Notify( -1, -1, "The lift must be connected to at least one other Sector.", NULL);
  2141.       ForgetSelection( &ldok);
  2142.       ForgetSelection( &ldflip);
  2143.       ForgetSelection( &ld1s);
  2144.       return;
  2145.    }
  2146.    /* flip the LineDefs that have the wrong orientation */
  2147.    if (ldflip != NULL)
  2148.       FlipLineDefs( ldflip, TRUE);
  2149.    /* merge the two selection lists */
  2150.    while (ldflip != NULL)
  2151.    {
  2152.       if (!IsSelected( ldok, ldflip->objnum))
  2153.      SelectObject( &ldok, ldflip->objnum);
  2154.       UnSelectObject( &ldflip, ldflip->objnum);
  2155.    }
  2156.    /* find a free tag number */
  2157.    tag = FindFreeTag();
  2158.    /* find the minimum altitude */
  2159.    ObjectsNeeded( OBJ_SECTORS, 0);
  2160.    minh = 32767;
  2161.    maxh = -32767;
  2162.    for (curs = sect; curs; curs = curs->next)
  2163.    {
  2164.       if (Sectors[ curs->objnum].floorh < minh)
  2165.      minh = Sectors[ curs->objnum].floorh;
  2166.       if (Sectors[ curs->objnum].floorh > maxh)
  2167.      maxh = Sectors[ curs->objnum].floorh;
  2168.    }
  2169.    ForgetSelection( §);
  2170.  
  2171.    /* change the Sector altitude if necessary */
  2172.    if (Sectors[ sector].floorh < maxh)
  2173.       Sectors[ sector].floorh = maxh;
  2174.  
  2175.    /* change the lift's ceiling height if necessary */
  2176.    if (Sectors[ sector].ceilh < maxh + 56)
  2177.       Sectors[ sector].ceilh = maxh + 56;     
  2178.  
  2179.    /* assign the new tag number to the lift */
  2180.    Sectors[ sector].tag = tag;
  2181.  
  2182.    /* change the LineDefs and SideDefs */
  2183.    while (ldok != NULL)
  2184.    {
  2185.       /* give the "lower lift" type and flags to the LineDef */
  2186.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2187.       n = ldok->objnum;
  2188.       LineDefs[ n].type = 62; /* lower lift (switch) */
  2189.       LineDefs[ n].flags = 0x04;
  2190.       LineDefs[ n].tag = tag;
  2191.       sd1 = LineDefs[ n].sidedef1;
  2192.       sd2 = LineDefs[ n].sidedef2;
  2193.       /* adjust the textures for the SideDefs visible from the outside */
  2194.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2195.       if (strncmp( SideDefs[ sd1].tex3, "-", 8))
  2196.       {
  2197.            if (!strncmp( SideDefs[ sd1].tex2, "-", 8))
  2198.               strncpy( SideDefs[ sd1].tex2, SideDefs[ sd1].tex3, 8);
  2199.            strncpy( SideDefs[ sd1].tex3, "-", 8);
  2200.       }
  2201.       if (!strncmp( SideDefs[ sd1].tex2, "-", 8))
  2202.            strncpy( SideDefs[ sd1].tex2, "SHAWN2", 8);
  2203.       /* adjust the textures for the SideDef visible from the lift */
  2204.       strncpy( SideDefs[ sd2].tex3, "-", 8);
  2205.       s = SideDefs[ sd1].sector;
  2206.       ObjectsNeeded( OBJ_SECTORS, 0);
  2207.       if (Sectors[ s].floorh > minh)
  2208.       {
  2209.            ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2210.      if (strncmp( SideDefs[ sd2].tex3, "-", 8))
  2211.      {
  2212.         if (!strncmp( SideDefs[ sd2].tex2, "-", 8))
  2213.            strncpy( SideDefs[ sd2].tex2, SideDefs[ sd1].tex3, 8);
  2214.         strncpy( SideDefs[ sd2].tex3, "-", 8);
  2215.      }
  2216.      if (!strncmp( SideDefs[ sd2].tex2, "-", 8))
  2217.         strncpy( SideDefs[ sd2].tex2, "SHAWN2", 8);
  2218.       }
  2219.       else
  2220.       {
  2221.      ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2222.      strncpy( SideDefs[ sd2].tex2, "-", 8);
  2223.       }
  2224.       strncpy( SideDefs[ sd2].tex3, "-", 8);
  2225.       ObjectsNeeded( OBJ_SECTORS, 0);
  2226.  
  2227.       /* if the ceiling of the Sector is lower than that of the lift */
  2228.       if (Sectors[ s].ceilh < Sectors[ sector].ceilh)
  2229.       {
  2230.            ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2231.            if (strncmp( SideDefs[ sd2].tex1, "-", 8))
  2232.               strncpy( SideDefs[ sd2].tex1, DefaultUpperTexture, 8);
  2233.       }
  2234.       ObjectsNeeded( OBJ_SECTORS, 0);
  2235.  
  2236.       /* if the floor of the Sector is above the lift */
  2237.       if (Sectors[ s].floorh >= Sectors[ sector].floorh)
  2238.       {
  2239.            ObjectsNeeded( OBJ_LINEDEFS, 0);
  2240.            LineDefs[ n].type = 88; /* lower lift (walk through) */
  2241.            /* flip it, just for fun */
  2242.            curs = NULL;
  2243.      SelectObject( &curs, n);
  2244.      FlipLineDefs( curs, TRUE);
  2245.      ForgetSelection( &curs);
  2246.       }
  2247.       /* done with this LineDef */
  2248.       UnSelectObject( &ldok, n);
  2249.    }
  2250.    while (ld1s != NULL)
  2251.    {
  2252.       /* these are the lift walls (one-sided) */
  2253.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2254.       n = ld1s->objnum;
  2255.       LineDefs[ n].flags = 0x01;
  2256.       sd1 = LineDefs[ n].sidedef1;
  2257.       /* adjust the textures for the SideDef */
  2258.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2259.       if (!strncmp( SideDefs[ sd1].tex3, "-", 8))
  2260.      strncpy( SideDefs[ sd1].tex3, DefaultWallTexture, 8);
  2261.       strncpy( SideDefs[ sd1].tex1, "-", 8);
  2262.       strncpy( SideDefs[ sd1].tex2, "-", 8);
  2263.       UnSelectObject( &ld1s, n);
  2264.    }
  2265. }
  2266.  
  2267.  
  2268.  
  2269. /*
  2270.    get the absolute height from which the textures are drawn
  2271. */
  2272.  
  2273. BCINT GetTextureRefHeight( BCINT sidedef) /* SWAP! */
  2274. {
  2275.    BCINT l, sector;
  2276.    BCINT otherside;
  2277.  
  2278.    /* find the SideDef on the other side of the LineDef, if any */
  2279.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  2280.    for (l = 0; l < NumLineDefs; l++)
  2281.    {
  2282.       if (LineDefs[ l].sidedef1 == sidedef)      {
  2283.      otherside = LineDefs[ l].sidedef2;
  2284.      break;
  2285.       }
  2286.       if (LineDefs[ l].sidedef2 == sidedef)
  2287.       {
  2288.      otherside = LineDefs[ l].sidedef1;
  2289.      break;
  2290.       }
  2291.    }
  2292.    /* get the Sector number */
  2293.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2294.    sector = SideDefs[ sidedef].sector;
  2295.    /* if the upper texture is displayed, then the reference is taken from the other Sector */
  2296.    if (otherside >= 0)
  2297.    {
  2298.       l = SideDefs[ otherside].sector;
  2299.       if (l > 0)                
  2300.       {
  2301.      ObjectsNeeded( OBJ_SECTORS, 0);
  2302.      if (Sectors[ l].ceilh < Sectors[ sector].ceilh && Sectors[ l].ceilh > Sectors[ sector].floorh)
  2303.         sector = l;
  2304.       }
  2305.    }
  2306.    /* return the altitude of the ceiling */
  2307.    ObjectsNeeded( OBJ_SECTORS, 0);
  2308.    if (sector >= 0)
  2309.       return Sectors[ sector].ceilh; /* textures are drawn from the ceiling down */
  2310.    else
  2311.       return 0; /* yuck! */
  2312. }
  2313.  
  2314.  
  2315.  
  2316. /*
  2317.    Align all textures for the given SideDefs
  2318.  
  2319.    Note from RQ:
  2320.       This function should be improved!
  2321.       But what should be improved first is the way the SideDefs are selected.
  2322.       It is stupid to change both sides of a wall when only one side needs
  2323.       to be changed.  But with the current selection method, there is no
  2324.       way to select only one side of a two-sided wall.
  2325. */
  2326.  
  2327. void AlignTexturesY( SelPtr *sdlist) /* SWAP! */
  2328. {
  2329.    BCINT h, refh;
  2330.  
  2331.    if (*sdlist == NULL)
  2332.       return;
  2333.    /* get the reference height from the first SideDef */
  2334.    refh = GetTextureRefHeight( (*sdlist)->objnum);
  2335.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2336.    SideDefs[ (*sdlist)->objnum].yoff = 0;
  2337.    UnSelectObject( sdlist, (*sdlist)->objnum);
  2338.    /* adjust Y offset in all other SideDefs */
  2339.    while (*sdlist != NULL)
  2340.    {
  2341.       h = GetTextureRefHeight( (*sdlist)->objnum);
  2342.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2343.       SideDefs[ (*sdlist)->objnum].yoff = (refh - h) % 128;
  2344.       UnSelectObject( sdlist, (*sdlist)->objnum);
  2345.    }
  2346.    MadeChanges = TRUE;
  2347. }
  2348.  
  2349.  
  2350.  
  2351. /*
  2352.    Function is to align all highlighted textures in the X-axis
  2353.  
  2354.    Note from RJH:
  2355.       LineDefs highlighted are read off in reverse order of highlighting.
  2356.       The '*sdlist' is in the reverse order of the above mentioned LineDefs
  2357.       i.e. the first LineDef SideDef you highlighted will be processed first.
  2358.  
  2359.    Note from RQ:
  2360.       See also the note for the previous function.
  2361. */
  2362.  
  2363. void AlignTexturesX( SelPtr *sdlist) /* SWAP! */
  2364. {
  2365.    char texname[ 9];    /* last texture name used in the highlited objects */
  2366.    BCINT ldef;                /* linedef number */
  2367.    BCINT sd1;             /* side one and side two of linedef */
  2368.    BCINT vert1, vert2;    /* vertex 1 and 2 for the linedef under scrutiny */
  2369.    BCINT xoffset;            /* xoffset accumulator */
  2370.    BCINT texlength;     /* the length of texture to format to */
  2371.    BCINT length;           /* length of linedef under scrutiny */
  2372.    char  errormessage[ 80];    /* area to hold the error messages produced */
  2373.    BCINT dummy;            /* holds useless data */
  2374.  
  2375.    vert1 = -1;
  2376.    vert2 = -1;        /* first time round the while loop the -1 value is needed */
  2377.    texlength = 0;
  2378.    xoffset = 0;
  2379.  
  2380.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2381.    strncpy( texname, SideDefs[ (*sdlist)->objnum].tex3, 8);
  2382.  
  2383.    /* test if there is a texture there */
  2384.    if (texname[0] == '-')
  2385.    {
  2386.       Beep();
  2387.       sprintf( errormessage, "No texture for SideDef #%d.", (*sdlist)->objnum);
  2388.       Notify( -1, -1, errormessage, NULL);
  2389.       return;
  2390.    }
  2391.  
  2392.    GetWallTextureSize( &texlength, &dummy, texname); /* clunky, but it works */
  2393.  
  2394.    while (*sdlist != NULL)
  2395.    {
  2396.       /* test if there is a texture there */
  2397.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2398.       if (strncmp( SideDefs[ (*sdlist)->objnum].tex3, texname,8))
  2399.       {
  2400.      Beep();
  2401.      sprintf( errormessage, "No texture for SideDef #%d.", (*sdlist)->objnum);
  2402.      Notify( -1, -1, errormessage, NULL);
  2403.      return;
  2404.       }
  2405.  
  2406.       sd1 = (*sdlist)->objnum;
  2407.       ldef = 0;
  2408.  
  2409.       ObjectsNeeded(OBJ_LINEDEFS,0);
  2410.       /* find out which LineDef holds that SideDef */
  2411.       while (LineDefs[ ldef].sidedef1 != sd1 && ldef < NumLineDefs)
  2412.      ldef++;
  2413.  
  2414.       vert1 = LineDefs[ ldef].start;
  2415.       /* test for linedef highlight continuity */
  2416.       if (vert1 != vert2 && vert2 != -1)
  2417.       {
  2418.      Beep();
  2419.      sprintf( errormessage, "LineDef #%d is not contiguous with the rest, please reselect.", (*sdlist)->objnum);
  2420.      Notify( -1, -1, errormessage, NULL);
  2421.      return;
  2422.       }
  2423.  
  2424.       /* put new xoffset into the SideDef */
  2425.       SideDefs[ sd1].xoff = xoffset;
  2426.  
  2427.       /* calculate length of LineDef */
  2428.       vert2 = LineDefs[ ldef].end;
  2429.       ObjectsNeeded( OBJ_VERTEXES, 0);
  2430.       length = ComputeDist( Vertexes[ vert2].x - Vertexes[ vert1].x, Vertexes[ vert2].y - Vertexes[ vert1].y);
  2431.  
  2432.       xoffset += length;
  2433.       /* remove multiples of texlength from xoffset */
  2434.       xoffset = xoffset % texlength;
  2435.       /* move to next object in selected list */
  2436.       UnSelectObject( sdlist, (*sdlist)->objnum);
  2437.    }
  2438.    MadeChanges = TRUE;
  2439. }
  2440.  
  2441.  
  2442.  
  2443. /*
  2444.    Distribute sector floor heights
  2445. */
  2446.  
  2447. void DistributeSectorFloors( SelPtr obj) /* SWAP! */
  2448. {
  2449.    SelPtr cur;
  2450.    BCINT  n, num, floor1h, floor2h;
  2451.  
  2452.    ObjectsNeeded( OBJ_SECTORS, 0);
  2453.  
  2454.    num = 0;
  2455.    for (cur = obj; cur->next; cur = cur->next)
  2456.       num++;
  2457.  
  2458.    floor1h = Sectors[ obj->objnum].floorh;
  2459.    floor2h = Sectors[ cur->objnum].floorh;
  2460.  
  2461.    n = 0;
  2462.    for (cur = obj; cur; cur = cur->next)
  2463.    {
  2464.       Sectors[ cur->objnum].floorh = floor1h + n * (floor2h - floor1h) / num;
  2465.       n++;
  2466.    }
  2467.    MadeChanges = TRUE;
  2468. }
  2469.  
  2470.  
  2471.  
  2472. /*
  2473.    Distribute sector ceiling heights
  2474. */
  2475.  
  2476. void DistributeSectorCeilings( SelPtr obj) /* SWAP! */
  2477. {
  2478.    SelPtr cur;
  2479.    BCINT n, num, ceil1h, ceil2h;
  2480.  
  2481.    ObjectsNeeded( OBJ_SECTORS, 0);
  2482.  
  2483.    num = 0;
  2484.    for (cur = obj; cur->next; cur = cur->next)
  2485.       num++;
  2486.  
  2487.    ceil1h = Sectors[ obj->objnum].ceilh;
  2488.    ceil2h = Sectors[ cur->objnum].ceilh;
  2489.  
  2490.    n = 0;
  2491.    for (cur = obj; cur; cur = cur->next)
  2492.    {
  2493.       Sectors[ cur->objnum].ceilh = ceil1h + n * (ceil2h - ceil1h) / num;
  2494.       n++;
  2495.    }
  2496.    MadeChanges = TRUE;
  2497. }
  2498.  
  2499.  
  2500.  
  2501. /* end of file */
  2502.