home *** CD-ROM | disk | FTP | other *** search
/ Crazy Collection 12 / CC-12_1.iso / update / doompack / data.a00 / ADE2.ZIP / SOURCE / SOURCE2.ZIP / OBJECTS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-08  |  85.8 KB  |  2,700 lines

  1. /*
  2.    Amazing Doom Editor, by Brendon Wyber and RaphaĆ«l Quinet.
  3.  
  4.    Doom II code by Adrian Cable.
  5.  
  6.    You are allowed to use any parts of this code in another program, as
  7.    long as you give credits to the authors in the documentation and in
  8.    the program itself.  Read the file README.1ST for more information.
  9.  
  10.    This program comes with absolutely no warranty.
  11.  
  12.    OBJECTS.C - object handling routines.
  13. */
  14.  
  15. /* the includes */
  16. #include "deu.h"
  17. #include "levels.h"
  18.  
  19.  
  20. /*
  21.    highlight the selected objects
  22. */
  23.  
  24. void HighlightSelection( int objtype, SelPtr list) /* SWAP! */
  25. {
  26.    SelPtr cur;
  27.  
  28.    if (list == NULL)
  29.       return;
  30.    for (cur = list; cur; cur = cur->next)
  31.       HighlightObject( objtype, cur->objnum, GREEN);
  32. }
  33.  
  34.  
  35.  
  36. /*
  37.    test if an object is in the selection list
  38. */
  39.  
  40. Bool IsSelected( SelPtr list, int objnum)
  41. {
  42.    SelPtr cur;
  43.  
  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, int objnum)
  57. {
  58.    SelPtr cur;
  59.  
  60.  
  61.    if (objnum < 0)
  62.       ProgError( "BUG: SelectObject called with %d", objnum);
  63.    cur = GetMemory( sizeof( struct SelectionList));
  64.    cur->next = *list;
  65.    cur->objnum = objnum;
  66.    *list = cur;
  67. }
  68.  
  69.  
  70.  
  71. /*
  72.    remove an object from the selection list
  73. */
  74.  
  75. void UnSelectObject( SelPtr *list, int objnum)
  76. {
  77.    SelPtr cur, prev;
  78.  
  79.    if (objnum < 0)
  80.       ProgError( "BUG: UnSelectObject called with %d", objnum);
  81.    prev = NULL;
  82.    cur = *list;
  83.    while (cur)
  84.    {
  85.       if (cur->objnum == objnum)
  86.       {
  87.          if (prev)
  88.             prev->next = cur->next;
  89.          else
  90.             *list = cur->next;
  91.          FreeMemory( cur);
  92.          if (prev)
  93.             cur = prev->next;
  94.          else
  95.             cur = NULL;
  96.       }
  97.       else
  98.       {
  99.          prev = cur;
  100.          cur = cur->next;
  101.       }
  102.    }
  103. }
  104.  
  105.  
  106.  
  107. /*
  108.    forget the selection list
  109. */
  110.  
  111. void ForgetSelection( SelPtr *list)
  112. {
  113.    SelPtr cur, prev;
  114.  
  115.    cur = *list;
  116.    while (cur)
  117.    {
  118.       prev = cur;
  119.       cur = cur->next;
  120.       FreeMemory( prev);
  121.    }
  122.    *list = NULL;
  123. }
  124.  
  125.  
  126.  
  127. /*
  128.    get the number of objets of a given type minus one
  129. */
  130. int GetMaxObjectNum( int objtype)
  131. {
  132.    switch (objtype)
  133.    {
  134.    case OBJ_THINGS:
  135.       return NumThings - 1;
  136.    case OBJ_LINEDEFS:
  137.       return NumLineDefs - 1;
  138.    case OBJ_SIDEDEFS:
  139.       return NumSideDefs - 1;
  140.    case OBJ_VERTEXES:
  141.       return NumVertexes - 1;
  142.    case OBJ_SEGS:
  143.       return NumSegs - 1;
  144.    case OBJ_SSECTORS:
  145.       return NumSSectors - 1;
  146.    case OBJ_SECTORS:
  147.       return NumSectors - 1;
  148.    }
  149.    return -1;
  150. }
  151.  
  152.  
  153. /*
  154.    check if there is something of interest inside the given box
  155. */
  156.  
  157. int GetCurObject( int objtype, int x0, int y0, int x1, int y1) /* SWAP! */
  158. {
  159.    int n, m, cur, curx;
  160.    int lx0, ly0, lx1, ly1;
  161.    int midx, midy;
  162.  
  163.    cur = -1;
  164.    if (x1 < x0)
  165.    {
  166.       n = x0;
  167.       x0 = x1;
  168.       x1 = n;
  169.    }
  170.    if (y1 < y0)
  171.    {
  172.       n = y0;
  173.       y0 = y1;
  174.       y1 = n;
  175.    }
  176.  
  177.    switch (objtype)
  178.    {
  179.    case OBJ_THINGS:
  180.       ObjectsNeeded( OBJ_THINGS, 0);
  181.       for (n = 0; n < NumThings; n++)
  182.          if (Things[ n].xpos >= x0 && Things[ n].xpos <= x1 && Things[ n].ypos >= y0 && Things[ n].ypos <= y1)
  183.          {
  184.             cur = n;
  185.             break;
  186.          }
  187.       break;
  188.    case OBJ_VERTEXES:
  189.       ObjectsNeeded( OBJ_VERTEXES, 0);
  190.       for (n = 0; n < NumVertexes; n++)
  191.          if (Vertexes[ n].x >= x0 && Vertexes[ n].x <= x1 && Vertexes[ n].y >= y0 && Vertexes[ n].y <= y1)
  192.          {
  193.             cur = n;
  194.             break;
  195.          }
  196.       break;
  197.    case OBJ_LINEDEFS:
  198.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  199.       for (n = 0; n < NumLineDefs; n++)
  200.       {
  201.          if (IsLineDefInside( n, x0, y0, x1, y1))
  202.          {
  203.             cur = n;
  204.             break;
  205.          }
  206.       }
  207.       break;
  208.    case OBJ_SECTORS:
  209.       /* hack, hack...  I look for the first LineDef crossing an horizontal half-line drawn from the cursor */
  210.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  211.       curx = MapMaxX + 1;
  212.       cur = -1;
  213.       midx = (x0 + x1) / 2;
  214.       midy = (y0 + y1) / 2;
  215.       for (n = 0; n < NumLineDefs; n++)
  216.          if ((Vertexes[ LineDefs[ n].start].y > midy) != (Vertexes[ LineDefs[ n].end].y > midy))
  217.          {
  218.             lx0 = Vertexes[ LineDefs[ n].start].x;
  219.             ly0 = Vertexes[ LineDefs[ n].start].y;
  220.             lx1 = Vertexes[ LineDefs[ n].end].x;
  221.             ly1 = Vertexes[ LineDefs[ n].end].y;
  222.             m = lx0 + (int) ((long) (midy - ly0) * (long) (lx1 - lx0) / (long) (ly1 - ly0));
  223.             if (m >= midx && m < curx)
  224.             {
  225.                curx = m;
  226.                cur = n;
  227.             }
  228.          }
  229.       /* now look if this LineDef has a SideDef bound to one sector */
  230.       if (cur >= 0)
  231.       {
  232.          if (Vertexes[ LineDefs[ cur].start].y > Vertexes[ LineDefs[ cur].end].y)
  233.             cur = LineDefs[ cur].sidedef1;
  234.          else
  235.             cur = LineDefs[ cur].sidedef2;
  236.          if (cur >= 0)
  237.          {
  238.             ObjectsNeeded( OBJ_SIDEDEFS, 0);
  239.             cur = SideDefs[ cur].sector;
  240.          }
  241.          else
  242.             cur = -1;
  243.       }
  244.       else
  245.          cur = -1;
  246.       break;
  247.    }
  248.    return cur;
  249. }
  250.  
  251.  
  252.  
  253. /*
  254.    select all objects inside a given box
  255. */
  256.  
  257. SelPtr SelectObjectsInBox( int objtype, int x0, int y0, int x1, int y1) /* SWAP! */
  258. {
  259.    int n, m;
  260.    SelPtr list;
  261.  
  262.    list = NULL;
  263.    if (x1 < x0)
  264.    {
  265.       n = x0;
  266.       x0 = x1;
  267.       x1 = n;
  268.    }
  269.    if (y1 < y0)
  270.    {
  271.       n = y0;
  272.       y0 = y1;
  273.       y1 = n;
  274.    }
  275.  
  276.    switch (objtype)
  277.    {
  278.    case OBJ_THINGS:
  279.       ObjectsNeeded( OBJ_THINGS, 0);
  280.       for (n = 0; n < NumThings; n++)
  281.          if (Things[ n].xpos >= x0 && Things[ n].xpos <= x1 && Things[ n].ypos >= y0 && Things[ n].ypos <= y1)
  282.             SelectObject( &list, n);
  283.       break;
  284.    case OBJ_VERTEXES:
  285.       ObjectsNeeded( OBJ_VERTEXES, 0);
  286.       for (n = 0; n < NumVertexes; n++)
  287.          if (Vertexes[ n].x >= x0 && Vertexes[ n].x <= x1 && Vertexes[ n].y >= y0 && Vertexes[ n].y <= y1)
  288.             SelectObject( &list, n);
  289.       break;
  290.    case OBJ_LINEDEFS:
  291.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  292.       for (n = 0; n < NumLineDefs; n++)
  293.       {
  294.          /* the two ends of the line must be in the box */
  295.          m = LineDefs[ n].start;
  296.          if (Vertexes[ m].x < x0 || Vertexes[ m].x > x1 || Vertexes[ m].y < y0 || Vertexes[ m].y > y1)
  297.             continue;
  298.          m = LineDefs[ n].end;
  299.          if (Vertexes[ m].x < x0 || Vertexes[ m].x > x1 || Vertexes[ m].y < y0 || Vertexes[ m].y > y1)
  300.             continue;
  301.          SelectObject( &list, n);
  302.       }
  303.       break;
  304.    case OBJ_SECTORS:
  305.       /* hack: select all sectors... */
  306.       for (n = 0; n < NumSectors; n++)
  307.          SelectObject( &list, n);
  308.       /* ... then remove the unwanted ones from the list */
  309.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, OBJ_VERTEXES, 0);
  310.       for (n = 0; n < NumLineDefs; n++)
  311.       {
  312.          m = LineDefs[ n].start;
  313.          if (Vertexes[ m].x < x0 || Vertexes[ m].x > x1 || Vertexes[ m].y < y0 || Vertexes[ m].y > y1)
  314.          {
  315.             m = LineDefs[ n].sidedef1;
  316.             if (m >= 0 && SideDefs[ m].sector >= 0)
  317.                UnSelectObject( &list, SideDefs[ m].sector);
  318.             m = LineDefs[ n].sidedef2;
  319.             if (m >= 0 && SideDefs[ m].sector >= 0)
  320.                UnSelectObject( &list, SideDefs[ m].sector);
  321.             continue;
  322.          }
  323.          m = LineDefs[ n].end;
  324.          if (Vertexes[ m].x < x0 || Vertexes[ m].x > x1 || Vertexes[ m].y < y0 || Vertexes[ m].y > y1)
  325.          {
  326.             m = LineDefs[ n].sidedef1;
  327.             if (m >= 0 && SideDefs[ m].sector >= 0)
  328.                UnSelectObject( &list, SideDefs[ m].sector);
  329.             m = LineDefs[ n].sidedef2;
  330.             if (m >= 0 && SideDefs[ m].sector >= 0)
  331.                UnSelectObject( &list, SideDefs[ m].sector);
  332.             continue;
  333.          }
  334.       }
  335.       break;
  336.    }
  337.    return list;
  338. }
  339.  
  340.  
  341.  
  342. /*
  343.    highlight the selected object
  344. */
  345.  
  346. void HighlightObject( int objtype, int objnum, int color) /* SWAP! */
  347. {
  348.    int  n, m;
  349.  
  350.    /* use XOR mode : drawing any line twice erases it */
  351.    setwritemode( XOR_PUT);
  352.    SetColor( color);
  353.    switch ( objtype)
  354.    {
  355.    case OBJ_THINGS:
  356.       ObjectsNeeded( OBJ_THINGS, 0);
  357.       m = (GetThingRadius( Things[ objnum].type) * 3) / 2;
  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.       DrawMapLine( Things[ objnum].xpos + m, Things[ objnum].ypos - m, Things[ objnum].xpos - m, Things[ objnum].ypos - m);
  362.       DrawMapArrow( Things[ objnum].xpos, Things[ objnum].ypos, Things[ objnum].angle * 182);
  363.       break;
  364.    case OBJ_LINEDEFS:
  365.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  366.       n = (Vertexes[ LineDefs[ objnum].start].x + Vertexes[ LineDefs[ objnum].end].x) / 2;
  367.       m = (Vertexes[ LineDefs[ objnum].start].y + Vertexes[ LineDefs[ objnum].end].y) / 2;
  368.       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);
  369.       setlinestyle(SOLID_LINE, 0, THICK_WIDTH);
  370.       DrawMapVector( Vertexes[ LineDefs[ objnum].start].x, Vertexes[ LineDefs[ objnum].start].y,
  371.                      Vertexes[ LineDefs[ objnum].end].x, Vertexes[ LineDefs[ objnum].end].y);
  372.       if (color != LIGHTRED && LineDefs[ objnum].tag > 0)
  373.       {
  374.          for (m = 0; m < NumSectors; m++)
  375.             if (Sectors[ m].tag == LineDefs[ objnum].tag)
  376.                HighlightObject( OBJ_SECTORS, m, LIGHTRED);
  377.       }
  378.       setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
  379.       break;
  380.    case OBJ_VERTEXES:
  381.       ObjectsNeeded( OBJ_VERTEXES, 0);
  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.       DrawMapLine( Vertexes[ objnum].x + OBJSIZE * 2, Vertexes[ objnum].y - OBJSIZE * 2, Vertexes[ objnum].x - OBJSIZE * 2, Vertexes[ objnum].y - OBJSIZE * 2);
  386.       break;
  387.    case OBJ_SECTORS:
  388.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, OBJ_VERTEXES, 0);
  389.       setlinestyle(SOLID_LINE, 0, THICK_WIDTH);
  390.       for (n = 0; n < NumLineDefs; n++)
  391.          if (SideDefs[ LineDefs[ n].sidedef1].sector == objnum || 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( int objtype, int 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( int objtype, SelPtr *list) /* SWAP! */
  429. {
  430.    int    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 = 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 = 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 = 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 = 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 = 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(int objtype, int copyfrom, int xpos, int ypos) /* SWAP! */
  614. {
  615.    int 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 = ResizeFarMemory( Things, (unsigned long) NumThings * sizeof( struct Thing));
  625.       else
  626.          Things = 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 = ResizeFarMemory( Vertexes, (unsigned long) NumVertexes * sizeof( struct Vertex));
  646.       else
  647.          Vertexes = 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 = ResizeFarMemory( LineDefs, (unsigned long) NumLineDefs * sizeof( struct LineDef));
  673.       else
  674.          LineDefs = 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 = ResizeFarMemory( SideDefs, (unsigned long) NumSideDefs * sizeof( struct SideDef));
  699.       else
  700.          SideDefs = 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 = ResizeFarMemory( Sectors, (unsigned long) NumSectors * sizeof( struct Sector));
  725.       else
  726.          Sectors = 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( int ldnum, int x0, int y0, int x1, int y1) /* SWAP - needs Vertexes & LineDefs */
  760. {
  761.    int lx0 = Vertexes[ LineDefs[ ldnum].start].x;
  762.    int ly0 = Vertexes[ LineDefs[ ldnum].start].y;
  763.    int lx1 = Vertexes[ LineDefs[ ldnum].end].x;
  764.    int ly1 = Vertexes[ LineDefs[ ldnum].end].y;
  765.    int 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 + (int) ( (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 + (int) ( (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 + (int) ( (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 + (int) ( (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. int GetOppositeSector( int ld1, Bool firstside) /* SWAP! */
  807. {
  808.    int x0, y0, dx0, dy0;
  809.    int x1, y1, dx1, dy1;
  810.    int x2, y2, dx2, dy2;
  811.    int ld2, dist;
  812.    int 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 + (int) ((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 + (int) ((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 + (int) ((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 + (int) ((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( int objtype, SelPtr obj) /* SWAP! */
  964. {
  965.    int        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( int objtype, SelPtr obj, int newx, int newy, int grid) /* SWAP! */
  1098. {
  1099.    int        n, m;
  1100.    int        dx, dy;
  1101.    SelPtr     cur, vertices;
  1102.    static int 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( int objtype, int objnum, int *xpos, int *ypos) /* SWAP! */
  1190. {
  1191.    int  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 = (int) ((accx + num / 2L) / num);
  1255.             *ypos = (int) ((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( int objtype, SelPtr obj, double angle, double scale) /* SWAP! */
  1273. {
  1274.    int    n, m;
  1275.    int    dx, dy;
  1276.    int    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 = (int) ((accx + num / 2L) / num);
  1297.          centery = (int) ((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 = (int) ((accx + num / 2L) / num);
  1319.          centery = (int) ((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. int FindFreeTag() /* SWAP! */
  1371. {
  1372.    int  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.    int    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.    int    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.    int    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;
  1531.    Bool   flipped, mergedone, isldend;
  1532.    int    v, refv;
  1533.    int    ld, sd;
  1534.    int    oldnumld;
  1535.  
  1536.    ObjectsNeeded( OBJ_VERTEXES, 0);
  1537.    confirmed = FALSE;
  1538.    redraw = FALSE;
  1539.    mergedone = FALSE;
  1540.    isldend = FALSE;
  1541.  
  1542.    /* first, check if two (or more) Vertices should be merged */
  1543.    ref = *list;
  1544.    while (ref)
  1545.    {
  1546.       refv = ref->objnum;
  1547.       ref = ref->next;
  1548.       /* check if there is a Vertex at the same position (same X and Y) */
  1549.       for (v = 0; v < NumVertexes; v++)
  1550.          if (v != refv && Vertexes[ refv].x == Vertexes[ v].x && Vertexes[ refv].y == Vertexes[ v].y)
  1551.          {
  1552.             redraw = TRUE;
  1553.             if (confirmed || Expert || Confirm( -1, -1, "Some Vertices occupy the same position", "Do you want to merge them into one?"))
  1554.             {
  1555.                /* don't ask for confirmation twice */
  1556.                confirmed = TRUE;
  1557.                /* merge the two vertices */
  1558.                mergedone = TRUE;
  1559.                cur = NULL;
  1560.                SelectObject( &cur, refv);
  1561.                SelectObject( &cur, v);
  1562.                MergeVertices( &cur);
  1563.                /* not useful but safer... */
  1564.                ObjectsNeeded( OBJ_VERTEXES, 0);
  1565.                /* update the references in the selection list */
  1566.                for (cur = *list; cur; cur = cur->next)
  1567.                   if (cur->objnum > refv)
  1568.                      cur->objnum = cur->objnum - 1;
  1569.                if (v > refv)
  1570.                   v--;
  1571.                /* the old Vertex has been deleted */
  1572.                UnSelectObject( list, refv);
  1573.                /* select the new Vertex instead */
  1574.                if (!IsSelected( *list, v))
  1575.                   SelectObject( list, v);
  1576.                break;
  1577.             }
  1578.             else
  1579.                return redraw;
  1580.          }
  1581.    }
  1582.    confirmed = FALSE;
  1583.  
  1584.    /* now, check if one or more Vertices are on a LineDef */
  1585.    ref = *list;
  1586.    while (ref)
  1587.    {
  1588.       refv = ref->objnum;
  1589.       ref = ref->next;
  1590.       oldnumld = NumLineDefs;
  1591.       /* check if this Vertex is on a LineDef */
  1592.       for (ld = 0; ld < oldnumld; ld++)
  1593.       {
  1594.          ObjectsNeeded( OBJ_VERTEXES, OBJ_LINEDEFS, 0);
  1595.          if (LineDefs[ ld].start == refv || LineDefs[ ld].end == refv)
  1596.          {
  1597.             /* one Vertex had a LineDef bound to it -- check it later */
  1598.             isldend = TRUE;
  1599.          }
  1600.          else if (IsLineDefInside( ld, Vertexes[ refv].x - 3, Vertexes[ refv].y - 3, Vertexes[ refv].x + 3, Vertexes[ refv].y + 3))
  1601.          {
  1602.             redraw = TRUE;
  1603.             if (confirmed || Expert || Confirm( -1, -1, "Some Vertices are on a LineDef", "Do you want to split the LineDef there?"))
  1604.             {
  1605.                /* don't ask for confirmation twice */
  1606.                confirmed = TRUE;
  1607.                /* split the LineDef */
  1608.                mergedone = TRUE;
  1609.                InsertObject( OBJ_LINEDEFS, ld, 0, 0);
  1610.                LineDefs[ ld].end = refv;
  1611.                LineDefs[ NumLineDefs - 1].start = refv;
  1612.                sd = LineDefs[ ld].sidedef1;
  1613.                if (sd >= 0)
  1614.                {
  1615.                   InsertObject( OBJ_SIDEDEFS, sd, 0, 0);
  1616.                   ObjectsNeeded( OBJ_LINEDEFS, 0);
  1617.                   LineDefs[ NumLineDefs - 1].sidedef1 = NumSideDefs - 1;
  1618.                }
  1619.                sd = LineDefs[ ld].sidedef2;
  1620.                if (sd >= 0)
  1621.                {
  1622.                   InsertObject( OBJ_SIDEDEFS, sd, 0, 0);
  1623.                   ObjectsNeeded( OBJ_LINEDEFS, 0);
  1624.                   LineDefs[ NumLineDefs - 1].sidedef2 = NumSideDefs - 1;
  1625.                }
  1626.                MadeChanges = TRUE;
  1627.                MadeMapChanges = TRUE;
  1628.             }
  1629.             else
  1630.                return redraw;
  1631.          }
  1632.       }
  1633.    }
  1634.  
  1635.    /* don't continue if this isn't necessary */
  1636.    if (isldend == FALSE || mergedone == FALSE)
  1637.       return redraw;
  1638.  
  1639.    confirmed = FALSE;
  1640.  
  1641.    /* finally, test if two LineDefs are between the same pair of Vertices */
  1642.    for (v = 0; v < NumLineDefs - 1; v++)
  1643.       for (ld = v + 1; ld < NumLineDefs; ld++)
  1644.          if ((LineDefs[ v].start == LineDefs[ ld].start && LineDefs[ v].end == LineDefs[ ld].end)
  1645.           || (LineDefs[ v].start == LineDefs[ ld].end && LineDefs[ v].end == LineDefs[ ld].start))
  1646.          {
  1647.             redraw = TRUE;
  1648.             if (confirmed || Expert || Confirm( -1, -1, "Some LineDefs are superimposed", "Do you want to merge them into one?"))
  1649.             {
  1650.                /* don't ask for confirmation twice */
  1651.                confirmed = TRUE;
  1652.                /* test if the LineDefs have the same orientation */
  1653.                if (LineDefs[ v].start == LineDefs[ ld].end)
  1654.                   flipped = TRUE;
  1655.                else
  1656.                   flipped = FALSE;
  1657.                /* merge the two LineDefs */
  1658.                if (LineDefs[ v].sidedef1 < 0)
  1659.                {
  1660.                   if (flipped)
  1661.                   {
  1662.                     LineDefs[ v].sidedef1 = LineDefs[ ld].sidedef2;
  1663.                     LineDefs[ ld].sidedef2 = -1;
  1664.                   }
  1665.                   else
  1666.                   {
  1667.                     LineDefs[ v].sidedef1 = LineDefs[ ld].sidedef1;
  1668.                     LineDefs[ ld].sidedef1 = -1;
  1669.                   }
  1670.                }
  1671.                if (LineDefs[ v].sidedef2 < 0)
  1672.                {
  1673.                   if (flipped)
  1674.                   {
  1675.                     LineDefs[ v].sidedef2 = LineDefs[ ld].sidedef1;
  1676.                     LineDefs[ ld].sidedef1 = -1;
  1677.                   }
  1678.                   else
  1679.                   {
  1680.                     LineDefs[ v].sidedef2 = LineDefs[ ld].sidedef2;
  1681.                     LineDefs[ ld].sidedef2 = -1;
  1682.                   }
  1683.                }
  1684.                if (LineDefs[ v].sidedef1 >= 0 && LineDefs[ v].sidedef2 >= 0 && (LineDefs[ v].flags & 0x04) == 0)
  1685.                   LineDefs[ v].flags = 0x04;
  1686.                DeleteObject( OBJ_LINEDEFS, ld);
  1687.             }
  1688.          }
  1689.    return redraw;
  1690. }
  1691.  
  1692.  
  1693.  
  1694. /*
  1695.    split one or more LineDefs in two, adding new Vertices in the middle
  1696. */
  1697.  
  1698. void SplitLineDefs( SelPtr obj) /* SWAP! */
  1699. {
  1700.    SelPtr cur;
  1701.    int    vstart, vend, sd;
  1702.  
  1703.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  1704.    for (cur = obj; cur; cur = cur->next)
  1705.    {
  1706.       vstart = LineDefs[ cur->objnum].start;
  1707.       vend = LineDefs[ cur->objnum].end;
  1708.       InsertObject( OBJ_VERTEXES, -1, (Vertexes[ vstart].x + Vertexes[ vend].x) / 2, (Vertexes[ vstart].y + Vertexes[ vend].y) / 2);
  1709.       InsertObject( OBJ_LINEDEFS, cur->objnum, 0, 0);
  1710.       LineDefs[ cur->objnum].end = NumVertexes - 1;
  1711.       LineDefs[ NumLineDefs - 1].start = NumVertexes - 1;
  1712.       sd = LineDefs[ cur->objnum].sidedef1;
  1713.       if (sd >= 0)
  1714.       {
  1715.          InsertObject( OBJ_SIDEDEFS, sd, 0, 0);
  1716.          ObjectsNeeded( OBJ_LINEDEFS, 0);
  1717.          LineDefs[ NumLineDefs - 1].sidedef1 = NumSideDefs - 1;
  1718.       }
  1719.       sd = LineDefs[ cur->objnum].sidedef2;
  1720.       if (sd >= 0)
  1721.       {
  1722.          InsertObject( OBJ_SIDEDEFS, sd, 0, 0);
  1723.          ObjectsNeeded( OBJ_LINEDEFS, 0);
  1724.          LineDefs[ NumLineDefs - 1].sidedef2 = NumSideDefs - 1;
  1725.       }
  1726.    }
  1727.    MadeChanges = TRUE;
  1728.    MadeMapChanges = TRUE;
  1729. }
  1730.  
  1731.  
  1732.  
  1733. /*
  1734.    split a Sector in two, adding a new LineDef between the two Vertices
  1735. */
  1736.  
  1737. void SplitSector( int vertex1, int vertex2) /* SWAP! */
  1738. {
  1739.    SelPtr llist;
  1740.    int    curv, s, l, sd;
  1741.    char   msg1[ 80], msg2[ 80];
  1742.  
  1743.    /* check if there is a Sector between the two Vertices (in the middle) */
  1744.    s = GetCurObject( OBJ_SECTORS, Vertexes[ vertex1].x, Vertexes[ vertex1].y, Vertexes[ vertex2].x, Vertexes[ vertex2].y);
  1745.    if (s < 0)
  1746.    {
  1747.       Beep();
  1748.       sprintf( msg1, "There is no Sector between Vertex #%d and Vertex #%d", vertex1, vertex2);
  1749.       Notify( -1, -1, msg1, NULL);
  1750.       return;
  1751.    }
  1752.    /* check if there is a closed path from vertex1 to vertex2, along the edge of the Sector s */
  1753.    ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1754.    llist = NULL;
  1755.    curv = vertex1;
  1756.    while (curv != vertex2)
  1757.    {
  1758.       for (l = 0; l < NumLineDefs; l++)
  1759.       {
  1760.          sd = LineDefs[ l].sidedef1;
  1761.          if (sd >= 0 && SideDefs[ sd].sector == s && LineDefs[ l].start == curv)
  1762.          {
  1763.             curv = LineDefs[ l].end;
  1764.             SelectObject( &llist, l);
  1765.             break;
  1766.          }
  1767.          sd = LineDefs[ l].sidedef2;
  1768.          if (sd >= 0 && SideDefs[ sd].sector == s && LineDefs[ l].end == curv)
  1769.          {
  1770.             curv = LineDefs[ l].start;
  1771.             SelectObject( &llist, l);
  1772.             break;
  1773.          }
  1774.       }
  1775.       if (l >= NumLineDefs)
  1776.       {
  1777.          Beep();
  1778.          sprintf( msg1, "Cannot find a closed path from Vertex #%d to Vertex #%d", vertex1, vertex2);
  1779.          if (curv == vertex1)
  1780.             sprintf( msg2, "There is no SideDef starting from Vertex #%d on Sector #%d", vertex1, s);
  1781.          else
  1782.             sprintf( msg2, "Check if Sector #%d is closed (cannot go past Vertex #%d)", s, curv);
  1783.          Notify( -1, -1, msg1, msg2);
  1784.          ForgetSelection( &llist);
  1785.          return;
  1786.       }
  1787.       if (curv == vertex1)
  1788.       {
  1789.          Beep();
  1790.          sprintf( msg1, "Vertex #%d is not on the same Sector (#%d) as Vertex #%d", vertex2, s, vertex1);
  1791.          Notify( -1, -1, msg1, NULL);
  1792.          ForgetSelection( &llist);
  1793.          return;
  1794.       }
  1795.    }
  1796.    /* now, the list of LineDefs for the new Sector is in llist */
  1797.  
  1798.    /* add the new Sector, LineDef and SideDefs */
  1799.    InsertObject( OBJ_SECTORS, s, 0, 0);
  1800.    InsertObject( OBJ_LINEDEFS, -1, 0, 0);
  1801.    LineDefs[ NumLineDefs - 1].start = vertex1;
  1802.    LineDefs[ NumLineDefs - 1].end = vertex2;
  1803.    LineDefs[ NumLineDefs - 1].flags = 4;
  1804.    InsertObject( OBJ_SIDEDEFS, -1, 0, 0);
  1805.    SideDefs[ NumSideDefs - 1].sector = s;
  1806.    strncpy( SideDefs[ NumSideDefs - 1].tex3, "-", 8);
  1807.    InsertObject( OBJ_SIDEDEFS, -1, 0, 0);
  1808.    strncpy( SideDefs[ NumSideDefs - 1].tex3, "-", 8);
  1809.    ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1810.    LineDefs[ NumLineDefs - 1].sidedef1 = NumSideDefs - 2;
  1811.    LineDefs[ NumLineDefs - 1].sidedef2 = NumSideDefs - 1;
  1812.  
  1813.    /* bind all LineDefs in llist to the new Sector */
  1814.    while (llist)
  1815.    {
  1816.       sd = LineDefs[ llist->objnum].sidedef1;
  1817.       if (sd < 0 || SideDefs[ sd].sector != s)
  1818.          sd = LineDefs[ llist->objnum].sidedef2;
  1819.       SideDefs[ sd].sector = NumSectors - 1;
  1820.       UnSelectObject( &llist, llist->objnum);
  1821.    }
  1822.  
  1823.    /* second check... uselful for Sectors within Sectors */
  1824.    ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1825.    for (l = 0; l < NumLineDefs; l++)
  1826.    {
  1827.       sd = LineDefs[ l].sidedef1;
  1828.       if (sd >= 0 && SideDefs[ sd].sector == s)
  1829.       {
  1830.          curv = GetOppositeSector( l, TRUE);
  1831.          ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1832.          if (curv == NumSectors - 1)
  1833.             SideDefs[ sd].sector = NumSectors - 1;
  1834.       }
  1835.       sd = LineDefs[ l].sidedef2;
  1836.       if (sd >= 0 && SideDefs[ sd].sector == s)
  1837.       {
  1838.          curv = GetOppositeSector( l, FALSE);
  1839.          ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1840.          if (curv == NumSectors - 1)
  1841.             SideDefs[ sd].sector = NumSectors - 1;
  1842.       }
  1843.    }
  1844.  
  1845.    MadeChanges = TRUE;
  1846.    MadeMapChanges = TRUE;
  1847. }
  1848.  
  1849.  
  1850.  
  1851. /*
  1852.    split two LineDefs, then split the Sector and add a new LineDef between the new Vertices
  1853. */
  1854.  
  1855. void SplitLineDefsAndSector( int linedef1, int linedef2) /* SWAP! */
  1856. {
  1857.    SelPtr llist;
  1858.    int    s1, s2, s3, s4;
  1859.    char   msg[ 80];
  1860.  
  1861.    /* check if the two LineDefs are adjacent to the same Sector */
  1862.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  1863.    s1 = LineDefs[ linedef1].sidedef1;
  1864.    s2 = LineDefs[ linedef1].sidedef2;
  1865.    s3 = LineDefs[ linedef2].sidedef1;
  1866.    s4 = LineDefs[ linedef2].sidedef2;
  1867.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1868.    if (s1 >= 0)
  1869.       s1 = SideDefs[ s1].sector;
  1870.    if (s2 >= 0)
  1871.       s2 = SideDefs[ s2].sector;
  1872.    if (s3 >= 0)
  1873.       s3 = SideDefs[ s3].sector;
  1874.    if (s4 >= 0)
  1875.       s4 = SideDefs[ s4].sector;
  1876.    if ((s1 < 0 || (s1 != s3 && s1 != s4)) && (s2 < 0 || (s2 != s3 && s2 != s4)))
  1877.    {
  1878.       Beep();
  1879.       sprintf( msg, "LineDefs #%d and #%d are not adjacent to the same Sector", linedef1, linedef2);
  1880.       Notify( -1, -1, msg, NULL);
  1881.       return;
  1882.    }
  1883.    /* split the two LineDefs and create two new Vertices */
  1884.    llist = NULL;
  1885.    SelectObject( &llist, linedef1);
  1886.    SelectObject( &llist, linedef2);
  1887.    SplitLineDefs( llist);
  1888.    ForgetSelection( &llist);
  1889.    /* split the Sector and create a LineDef between the two Vertices */
  1890.    SplitSector( NumVertexes - 1, NumVertexes - 2);
  1891. }
  1892.  
  1893.  
  1894.  
  1895. /*
  1896.    merge two or more Sectors into one
  1897. */
  1898.  
  1899. void MergeSectors( SelPtr *slist) /* SWAP! */
  1900. {
  1901.   SelPtr cur;
  1902.   int    n, olds, news;
  1903.  
  1904.   /* save the first Sector number */
  1905.   news = (*slist)->objnum;
  1906.   UnSelectObject( slist, news);
  1907.   ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1908.  
  1909.   /* change all SideDefs references to the other Sectors */
  1910.   for (cur = *slist; cur; cur = cur->next)
  1911.   {
  1912.      olds = cur->objnum;
  1913.      for (n = 0; n < NumSideDefs; n++)
  1914.      {
  1915.         if (SideDefs[ n].sector == olds)
  1916.            SideDefs[ n].sector = news;
  1917.      }
  1918.   }
  1919.  
  1920.   /* delete the Sectors */
  1921.   DeleteObjects( OBJ_SECTORS, slist);
  1922.  
  1923.   /* the returned list contains only the first Sector */
  1924.   SelectObject( slist, news);
  1925. }
  1926.  
  1927.  
  1928.  
  1929. /*
  1930.    delete one or several two-sided LineDefs and join the two Sectors
  1931. */
  1932.  
  1933. void DeleteLineDefsJoinSectors( SelPtr *ldlist) /* SWAP! */
  1934. {
  1935.    SelPtr cur, slist;
  1936.    int    sd1, sd2;
  1937.    int    s1, s2;
  1938.    char   msg[ 80];
  1939.  
  1940.    /* first, do the tests for all LineDefs */
  1941.    for (cur = *ldlist; cur; cur = cur->next)
  1942.    {
  1943.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  1944.       sd1 = LineDefs[ cur->objnum].sidedef1;
  1945.       sd2 = LineDefs[ cur->objnum].sidedef2;
  1946.       if (sd1 < 0 || sd2 < 0)
  1947.       {
  1948.          Beep();
  1949.          sprintf( msg, "ERROR: LineDef #%d has only one side", cur->objnum);
  1950.          Notify( -1, -1, msg, NULL);
  1951.          return;
  1952.       }
  1953.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1954.       s1 = SideDefs[ sd1].sector;
  1955.       s2 = SideDefs[ sd2].sector;
  1956.       if (s1 < 0 || s2 < 0)
  1957.       {
  1958.          Beep();
  1959.          sprintf( msg, "ERROR: LineDef #%d has two sides, but one", cur->objnum);
  1960.          Notify( -1, -1, msg, "side is not bound to any Sector");
  1961.          return;
  1962.       }
  1963.    }
  1964.  
  1965.    /* then join the Sectors and delete the LineDefs */
  1966.    for (cur = *ldlist; cur; cur = cur->next)
  1967.    {
  1968.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  1969.       sd1 = LineDefs[ cur->objnum].sidedef1;
  1970.       sd2 = LineDefs[ cur->objnum].sidedef2;
  1971.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1972.       s1 = SideDefs[ sd1].sector;
  1973.       s2 = SideDefs[ sd2].sector;
  1974.       slist = NULL;
  1975.       SelectObject( &slist, s2);
  1976.       SelectObject( &slist, s1);
  1977.       MergeSectors( &slist);
  1978.       ForgetSelection( &slist);
  1979.    }
  1980.    DeleteObjects( OBJ_LINEDEFS, ldlist);
  1981. }
  1982.  
  1983.  
  1984.  
  1985.  
  1986. /*
  1987.    turn a Sector into a door: change the LineDefs and SideDefs
  1988. */
  1989.  
  1990. void MakeDoorFromSector( int sector) /* SWAP! */
  1991. {
  1992.    int    sd1, sd2;
  1993.    int    n, s;
  1994.    SelPtr ldok, ldflip, ld1s;
  1995.  
  1996.    ldok = NULL;
  1997.    ldflip = NULL;
  1998.    ld1s = NULL;
  1999.    s = 0;
  2000.    /* build lists of LineDefs that border the Sector */
  2001.    for (n = 0; n < NumLineDefs; n++)
  2002.    {
  2003.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2004.       sd1 = LineDefs[ n].sidedef1;
  2005.       sd2 = LineDefs[ n].sidedef2;
  2006.       if (sd1 >= 0 && sd2 >= 0)
  2007.       {
  2008.          ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2009.          if (SideDefs[ sd2].sector == sector)
  2010.          {
  2011.             SelectObject( &ldok, n); /* already ok */
  2012.             s++;
  2013.          }
  2014.          if (SideDefs[ sd1].sector == sector)
  2015.          {
  2016.             SelectObject( &ldflip, n); /* must be flipped */
  2017.             s++;
  2018.          }
  2019.       }
  2020.       else if (sd1 >= 0 && sd2 < 0)
  2021.       {
  2022.          ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2023.          if (SideDefs[ sd1].sector == sector)
  2024.             SelectObject( &ld1s, n); /* wall (one-sided) */
  2025.       }
  2026.    }
  2027.    /* a normal door has two sides... */
  2028.    if (s < 2)
  2029.    {
  2030.       Beep();
  2031.       Notify( -1, -1, "The door must be connected to two other Sectors.", NULL);
  2032.       ForgetSelection( &ldok);
  2033.       ForgetSelection( &ldflip);
  2034.       ForgetSelection( &ld1s);
  2035.       return;
  2036.    }
  2037.    if ((s > 2) && !(Expert || Confirm( -1, -1, "The door will have more than two sides.", "Do you still want to create it?")))
  2038.    {
  2039.       ForgetSelection( &ldok);
  2040.       ForgetSelection( &ldflip);
  2041.       ForgetSelection( &ld1s);
  2042.       return;
  2043.    }
  2044.    /* flip the LineDefs that have the wrong orientation */
  2045.    if (ldflip != NULL)
  2046.       FlipLineDefs( ldflip, TRUE);
  2047.    /* merge the two selection lists */
  2048.    while (ldflip != NULL)
  2049.    {
  2050.       if (!IsSelected( ldok, ldflip->objnum))
  2051.          SelectObject( &ldok, ldflip->objnum);
  2052.       UnSelectObject( &ldflip, ldflip->objnum);
  2053.    }
  2054.    /* change the LineDefs and SideDefs */
  2055.    while (ldok != NULL)
  2056.    {
  2057.       /* give the "normal door" type and flags to the LineDef */
  2058.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2059.       n = ldok->objnum;
  2060.       LineDefs[ n].type = 1;
  2061.       LineDefs[ n].flags = 0x04;
  2062.       sd1 = LineDefs[ n].sidedef1; /* outside */
  2063.       sd2 = LineDefs[ n].sidedef2; /* inside */
  2064.       /* adjust the textures for the SideDefs */
  2065.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2066.       if (strncmp( SideDefs[ sd1].tex3, "-", 8))
  2067.       {
  2068.          if (!strncmp( SideDefs[ sd1].tex1, "-", 8))
  2069.             strncpy( SideDefs[ sd1].tex1, SideDefs[ sd1].tex3, 8);
  2070.          strncpy( SideDefs[ sd1].tex3, "-", 8);
  2071.       }
  2072.       if (!strncmp( SideDefs[ sd1].tex1, "-", 8))
  2073.          strncpy( SideDefs[ sd1].tex1, "BIGDOOR2", 8);
  2074.       strncpy( SideDefs[ sd2].tex3, "-", 8);
  2075.       UnSelectObject( &ldok, n);
  2076.    }
  2077.    while (ld1s != NULL)
  2078.    {
  2079.       /* give the "door side" flags to the LineDef */
  2080.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2081.       n = ld1s->objnum;
  2082.       LineDefs[ n].flags = 0x11;
  2083.       sd1 = LineDefs[ n].sidedef1;
  2084.       /* adjust the textures for the SideDef */
  2085.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2086.       if (!strncmp( SideDefs[ sd1].tex3, "-", 8))
  2087.          strncpy( SideDefs[ sd1].tex3, "DOORTRAK", 8);
  2088.       strncpy( SideDefs[ sd1].tex1, "-", 8);
  2089.       strncpy( SideDefs[ sd1].tex2, "-", 8);
  2090.       UnSelectObject( &ld1s, n);
  2091.    }
  2092.    /* adjust the ceiling height */
  2093.    ObjectsNeeded( OBJ_SECTORS, 0);
  2094.    Sectors[ sector].ceilh = Sectors[ sector].floorh;
  2095. }
  2096.  
  2097.  
  2098.  
  2099. /*
  2100.    turn a Sector into a lift: change the LineDefs and SideDefs
  2101. */
  2102.  
  2103. void MakeLiftFromSector( int sector) /* SWAP! */
  2104. {
  2105.    int    sd1, sd2;
  2106.    int    n, s, tag;
  2107.    SelPtr ldok, ldflip, ld1s;
  2108.    SelPtr sect, curs;
  2109.    int    minh, maxh;
  2110.  
  2111.    ldok = NULL;
  2112.    ldflip = NULL;
  2113.    ld1s = NULL;
  2114.    sect = NULL;
  2115.    /* build lists of LineDefs that border the Sector */
  2116.    for (n = 0; n < NumLineDefs; n++)
  2117.    {
  2118.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2119.       sd1 = LineDefs[ n].sidedef1;
  2120.       sd2 = LineDefs[ n].sidedef2;
  2121.       if (sd1 >= 0 && sd2 >= 0)
  2122.       {
  2123.          ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2124.          if (SideDefs[ sd2].sector == sector)
  2125.          {
  2126.             SelectObject( &ldok, n); /* already ok */
  2127.             s = SideDefs[ sd1].sector;
  2128.             if (s != sector && !IsSelected( sect, s))
  2129.                SelectObject( §, s);
  2130.          }
  2131.          if (SideDefs[ sd1].sector == sector)
  2132.          {
  2133.             SelectObject( &ldflip, n); /* will be flipped */
  2134.             s = SideDefs[ sd2].sector;
  2135.             if (s != sector && !IsSelected( sect, s))
  2136.                SelectObject( §, s);
  2137.          }
  2138.       }
  2139.       else if (sd1 >= 0 && sd2 < 0)
  2140.       {
  2141.          ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2142.          if (SideDefs[ sd1].sector == sector)
  2143.             SelectObject( &ld1s, n); /* wall (one-sided) */
  2144.       }
  2145.    }
  2146.    /* there must be a way to go on the lift... */
  2147.    if (sect == NULL)
  2148.    {
  2149.       Beep();
  2150.       Notify( -1, -1, "The lift must be connected to at least one other Sector.", NULL);
  2151.       ForgetSelection( &ldok);
  2152.       ForgetSelection( &ldflip);
  2153.       ForgetSelection( &ld1s);
  2154.       return;
  2155.    }
  2156.    /* flip the LineDefs that have the wrong orientation */
  2157.    if (ldflip != NULL)
  2158.       FlipLineDefs( ldflip, TRUE);
  2159.    /* merge the two selection lists */
  2160.    while (ldflip != NULL)
  2161.    {
  2162.       if (!IsSelected( ldok, ldflip->objnum))
  2163.          SelectObject( &ldok, ldflip->objnum);
  2164.       UnSelectObject( &ldflip, ldflip->objnum);
  2165.    }
  2166.  
  2167.    /* find a free tag number */
  2168.    tag = FindFreeTag();
  2169.  
  2170.    /* find the minimum and maximum altitudes */
  2171.    ObjectsNeeded( OBJ_SECTORS, 0);
  2172.    minh = 32767;
  2173.    maxh = -32767;
  2174.    for (curs = sect; curs; curs = curs->next)
  2175.    {
  2176.       if (Sectors[ curs->objnum].floorh < minh)
  2177.          minh = Sectors[ curs->objnum].floorh;
  2178.       if (Sectors[ curs->objnum].floorh > maxh)
  2179.          maxh = Sectors[ curs->objnum].floorh;
  2180.    }
  2181.    ForgetSelection( §);
  2182.  
  2183.    /* change the lift's floor height if necessary */
  2184.    if (Sectors[ sector].floorh < maxh)
  2185.       Sectors[ sector].floorh = maxh;
  2186.  
  2187.    /* change the lift's ceiling height if necessary */
  2188.    if (Sectors[ sector].ceilh < maxh + 56)
  2189.       Sectors[ sector].ceilh = maxh + 56;
  2190.  
  2191.    /* assign the new tag number to the lift */
  2192.    Sectors[ sector].tag = tag;
  2193.  
  2194.    /* change the LineDefs and SideDefs */
  2195.    while (ldok != NULL)
  2196.    {
  2197.       /* give the "lower lift" type and flags to the LineDef */
  2198.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2199.       n = ldok->objnum;
  2200.       LineDefs[ n].type = 62; /* lower lift (switch) */
  2201.       LineDefs[ n].flags = 0x04;
  2202.       LineDefs[ n].tag = tag;
  2203.       sd1 = LineDefs[ n].sidedef1; /* outside */
  2204.       sd2 = LineDefs[ n].sidedef2; /* inside */
  2205.       /* adjust the textures for the SideDef visible from the outside */
  2206.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2207.       if (strncmp( SideDefs[ sd1].tex3, "-", 8))
  2208.       {
  2209.          if (!strncmp( SideDefs[ sd1].tex2, "-", 8))
  2210.             strncpy( SideDefs[ sd1].tex2, SideDefs[ sd1].tex3, 8);
  2211.          strncpy( SideDefs[ sd1].tex3, "-", 8);
  2212.       }
  2213.       if (!strncmp( SideDefs[ sd1].tex2, "-", 8))
  2214.          strncpy( SideDefs[ sd1].tex2, "SHAWN2", 8);
  2215.       /* adjust the textures for the SideDef visible from the lift */
  2216.       strncpy( SideDefs[ sd2].tex3, "-", 8);
  2217.       s = SideDefs[ sd1].sector;
  2218.       ObjectsNeeded( OBJ_SECTORS, 0);
  2219.       if (Sectors[ s].floorh > minh)
  2220.       {
  2221.          ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2222.          if (strncmp( SideDefs[ sd2].tex3, "-", 8))
  2223.          {
  2224.             if (!strncmp( SideDefs[ sd2].tex2, "-", 8))
  2225.                strncpy( SideDefs[ sd2].tex2, SideDefs[ sd1].tex3, 8);
  2226.             strncpy( SideDefs[ sd2].tex3, "-", 8);
  2227.          }
  2228.          if (!strncmp( SideDefs[ sd2].tex2, "-", 8))
  2229.             strncpy( SideDefs[ sd2].tex2, "SHAWN2", 8);
  2230.       }
  2231.       else
  2232.       {
  2233.          ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2234.          strncpy( SideDefs[ sd2].tex2, "-", 8);
  2235.       }
  2236.       strncpy( SideDefs[ sd2].tex3, "-", 8);
  2237.       ObjectsNeeded( OBJ_SECTORS, 0);
  2238.       /* if the ceiling of the Sector is lower than that of the lift */
  2239.       if (Sectors[ s].ceilh < Sectors[ sector].ceilh)
  2240.       {
  2241.          ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2242.          if (strncmp( SideDefs[ sd2].tex1, "-", 8))
  2243.             strncpy( SideDefs[ sd2].tex1, DefaultUpperTexture, 8);
  2244.       }
  2245.       ObjectsNeeded( OBJ_SECTORS, 0);
  2246.       /* if the floor of the Sector is above the lift */
  2247.       if (Sectors[ s].floorh >= Sectors[ sector].floorh)
  2248.       {
  2249.          ObjectsNeeded( OBJ_LINEDEFS, 0);
  2250.          LineDefs[ n].type = 88; /* lower lift (walk through) */
  2251.          /* flip it, just for fun */
  2252.          curs = NULL;
  2253.          SelectObject( &curs, n);
  2254.          FlipLineDefs( curs, TRUE);
  2255.          ForgetSelection( &curs);
  2256.       }
  2257.       /* done with this LineDef */
  2258.       UnSelectObject( &ldok, n);
  2259.    }
  2260.  
  2261.    while (ld1s != NULL)
  2262.    {
  2263.       /* these are the lift walls (one-sided) */
  2264.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  2265.       n = ld1s->objnum;
  2266.       LineDefs[ n].flags = 0x01;
  2267.       sd1 = LineDefs[ n].sidedef1;
  2268.       /* adjust the textures for the SideDef */
  2269.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2270.       if (!strncmp( SideDefs[ sd1].tex3, "-", 8))
  2271.          strncpy( SideDefs[ sd1].tex3, DefaultWallTexture, 8);
  2272.       strncpy( SideDefs[ sd1].tex1, "-", 8);
  2273.       strncpy( SideDefs[ sd1].tex2, "-", 8);
  2274.       UnSelectObject( &ld1s, n);
  2275.    }
  2276. }
  2277.  
  2278.  
  2279.  
  2280. /*
  2281.    get the absolute height from which the textures are drawn
  2282. */
  2283.  
  2284. int GetTextureRefHeight( int sidedef) /* SWAP! */
  2285. {
  2286.    int l, sector;
  2287.    int otherside;
  2288.  
  2289.    /* find the SideDef on the other side of the LineDef, if any */
  2290.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  2291.    for (l = 0; l < NumLineDefs; l++)
  2292.    {
  2293.       if (LineDefs[ l].sidedef1 == sidedef)
  2294.       {
  2295.          otherside = LineDefs[ l].sidedef2;
  2296.          break;
  2297.       }
  2298.       if (LineDefs[ l].sidedef2 == sidedef)
  2299.       {
  2300.          otherside = LineDefs[ l].sidedef1;
  2301.          break;
  2302.       }
  2303.    }
  2304.    /* get the Sector number */
  2305.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2306.    sector = SideDefs[ sidedef].sector;
  2307.    /* if the upper texture is displayed, then the reference is taken from the other Sector */
  2308.    if (otherside >= 0)
  2309.    {
  2310.       l = SideDefs[ otherside].sector;
  2311.       if (l > 0)
  2312.       {
  2313.          ObjectsNeeded( OBJ_SECTORS, 0);
  2314.          if (Sectors[ l].ceilh < Sectors[ sector].ceilh && Sectors[ l].ceilh > Sectors[ sector].floorh)
  2315.             sector = l;
  2316.       }
  2317.    }
  2318.    /* return the altitude of the ceiling */
  2319.    ObjectsNeeded( OBJ_SECTORS, 0);
  2320.    if (sector >= 0)
  2321.       return Sectors[ sector].ceilh; /* textures are drawn from the ceiling down */
  2322.    else
  2323.       return 0; /* yuck! */
  2324. }
  2325.  
  2326.  
  2327.  
  2328. /*
  2329.    Align all textures for the given SideDefs
  2330.  
  2331.    Note from RQ:
  2332.       This function should be improved!
  2333.       But what should be improved first is the way the SideDefs are selected.
  2334.       It is stupid to change both sides of a wall when only one side needs
  2335.       to be changed.  But with the current selection method, there is no
  2336.       way to select only one side of a two-sided wall.
  2337. */
  2338.  
  2339. void AlignTexturesY( SelPtr *sdlist) /* SWAP! */
  2340. {
  2341.    int h, refh;
  2342.  
  2343.    if (*sdlist == NULL)
  2344.       return;
  2345.  
  2346.    /* get the reference height from the first SideDef */
  2347.    refh = GetTextureRefHeight( (*sdlist)->objnum);
  2348.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2349.    SideDefs[ (*sdlist)->objnum].yoff = 0;
  2350.    UnSelectObject( sdlist, (*sdlist)->objnum);
  2351.  
  2352.    /* adjust Y offset in all other SideDefs */
  2353.    while (*sdlist != NULL)
  2354.    {
  2355.       h = GetTextureRefHeight( (*sdlist)->objnum);
  2356.       ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2357.       SideDefs[ (*sdlist)->objnum].yoff = (refh - h) % 128;
  2358.       UnSelectObject( sdlist, (*sdlist)->objnum);
  2359.    }
  2360.    MadeChanges = TRUE;
  2361. }
  2362.  
  2363.  
  2364.  
  2365. /*
  2366.         Function is to align all highlighted textures in the X-axis
  2367.  
  2368.         Note from RJH:
  2369.                 LineDefs highlighted are read off in reverse order of highlighting.
  2370.                 The '*sdlist' is in the reverse order of the above mentioned LineDefs
  2371.                 i.e. the first LineDef SideDefs you highlighted will be processed first.
  2372.  
  2373.         Note from RQ:
  2374.                 See also the note for the previous function.
  2375.  
  2376.         Note from RJH:
  2377.                 For the menu for aligning textures 'X' NOW operates upon the fact that
  2378.                 ALL the SIDEDEFS from the selected LINEDEFS are in the *SDLIST, 2nd
  2379.                 SideDef is first, 1st SideDef is 2nd). Aligning textures X now does
  2380.                 SIDEDEF 1's and SIDEDEF 2's.  If the selection process is changed,
  2381.                 the following needs to be altered radically.
  2382. */
  2383.  
  2384. void AlignTexturesX(SelPtr *sdlist) /* SWAP! */
  2385. {
  2386.         char texname[9];        /* FIRST texture name used in the highlited objects */
  2387.         char errormessage[80];  /* area to hold the error messages produced */
  2388.         int  ldef;              /* linedef number */
  2389.         int  sd1;               /* current SideDef in *sdlist */
  2390.         int  vert1, vert2;      /* vertex 1 and 2 for the linedef under scrutiny */
  2391.         int  xoffset;           /* xoffset accumulator */
  2392.         int  useroffset;        /* user input offset for first input */
  2393.         int  texlength;         /* the length of texture to format to */
  2394.         int  length;            /* length of linedef under scrutiny */
  2395.         int  dummy;             /* holds useless data */
  2396.         int  type_off;          /* do we have an initial offset to use */
  2397.         int  type_tex;          /* do we check for same textures */
  2398.         int  type_sd;           /* do we align SideDef 1 or SideDef2 */
  2399.  
  2400.         type_sd  = 0;     /* which SideDef to align, 1 = SideDef1, 2 = SideDef2 */
  2401.         type_tex = 0;     /* do we test for similar textures, 0 = inactive, 1 = active */
  2402.         type_off = 0;     /* do we have an inital offset, 0 = inactive, 1 = active */
  2403.  
  2404.         vert1   = -1;
  2405.         vert2   = -1;           /* first time round the while loop the -1 value is needed */
  2406.         texlength  = 0;
  2407.         xoffset    = 0;
  2408.         useroffset = 0;
  2409.  
  2410.         switch(DisplayMenu( 250, 110, "Aligning textures 'Y' menu:",
  2411.  
  2412.                           " SideDef 1,  Check for identical textures.     ",
  2413.                           " SideDef 1,  As above, but with inital offset. ",
  2414.                           " SideDef 1,  No texture checking.              ",
  2415.                           " SideDef 1,  As above, but with inital offset. ",
  2416.  
  2417.                           " SideDef 2, Check for identical textures.     ",
  2418.                           " SideDef 2, As above, but with inital offset. ",
  2419.                           " SideDef 2, No texture checking.              ",
  2420.                           " SideDef 2, As above, but with inital offset. ",
  2421.                           NULL))
  2422.                 {
  2423.                  case 1:                /* SideDef 1 with checking for same textures   */
  2424.                         type_sd = 1; type_tex = 1; type_off = 0;
  2425.                         break;
  2426.  
  2427.                  case 2:                /* SideDef 1 as above, but with inital offset  */
  2428.                         type_sd = 1; type_tex = 1; type_off = 1;
  2429.                         break;
  2430.  
  2431.                  case 3:                /* SideDef 1 regardless of same textures       */
  2432.                         type_sd = 1; type_tex = 0; type_off = 0;
  2433.                         break;
  2434.  
  2435.                  case 4:                /* SideDef 1 as above, but with inital offset  */
  2436.                         type_sd = 1; type_tex = 0; type_off = 1;
  2437.                         break;
  2438.  
  2439.                  case 5:                /* SideDef 2 with checking for same textures   */
  2440.                         type_sd = 2; type_tex = 1; type_off = 0;
  2441.                         break;
  2442.  
  2443.                  case 6:                /* SideDef 2 as above, but with initial offset */
  2444.                         type_sd = 2; type_tex = 1; type_off = 1;
  2445.                         break;
  2446.  
  2447.                  case 7:                /* SideDef 2 regardless of same textures       */
  2448.                         type_sd = 2; type_tex = 0; type_off = 0;
  2449.                         break;
  2450.  
  2451.                  case 8: /* SideDef 2 as above, but with initial offset       */
  2452.                         type_sd = 2; type_tex = 0; type_off = 1;
  2453.                         break;
  2454.                 }
  2455.  
  2456.         ldef = 0;
  2457.         sd1 = (*sdlist) ->objnum;
  2458.  
  2459.         if(type_sd == 1) /* throw out all 2nd SideDefs untill a 1st is found */
  2460.         {
  2461.                 while((*sdlist)!=NULL && LineDefs[ldef].sidedef1!=sd1 && ldef<=NumLineDefs)
  2462.                 {
  2463.                         ldef++;
  2464.                         if(LineDefs[ldef].sidedef2 == sd1)
  2465.                         {
  2466.                                 UnSelectObject(sdlist, (*sdlist)->objnum);
  2467.                                 sd1 = (*sdlist) ->objnum;
  2468.                                 ldef = 0;
  2469.                                 if((*sdlist)==NULL)
  2470.                                         return;
  2471.                         }
  2472.                 }
  2473.         }
  2474.  
  2475.         if(type_sd == 2) /* throw out all 1st SideDefs untill a 2nd is found */
  2476.         {
  2477.                 while(LineDefs[ldef].sidedef2!=sd1 && ldef<=NumLineDefs)
  2478.                 {
  2479.                         ldef++;
  2480.                         if(LineDefs[ldef].sidedef1 == sd1)
  2481.                         {
  2482.                                 UnSelectObject(sdlist, (*sdlist)->objnum);
  2483.                                 sd1 = (*sdlist) ->objnum;
  2484.                                 ldef = 0;
  2485.                                 if((*sdlist) == NULL)
  2486.                                         return;
  2487.                         }
  2488.                 }
  2489.         }
  2490.  
  2491.         ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2492.  
  2493.         /* get texture name of the SideDef in the *sdlist) */
  2494.         strncpy( texname, SideDefs[ (*sdlist)->objnum].tex3, 8);
  2495.  
  2496.         /* test if there is a texture there */
  2497.         if (texname[0] == '-')
  2498.         {
  2499.                 Beep();
  2500.                 sprintf( errormessage, "No texture for SideDef #%d.", (*sdlist)->objnum);
  2501.                 Notify( -1, -1, errormessage, NULL);
  2502.                 return;
  2503.         }
  2504.  
  2505.         GetWallTextureSize( &texlength, &dummy, texname); /* clunky, but it works */
  2506.  
  2507.         /* get initial offset to use (if requrired) */
  2508.         if(type_off == 1)    /* source taken from InputObjectNumber */
  2509.         {
  2510.                 int  x0;          /* left hand (x) window start     */
  2511.                 int  y0;                      /* top (y) window start           */
  2512.                 int  key;                       /* holds value returned by InputInteger */
  2513.                 char prompt[80];  /* prompt for inital offset input */
  2514.  
  2515.                 if(UseMouse)
  2516.                         HideMousePointer();
  2517.  
  2518.                 sprintf(prompt, "Enter initial offset between 0 and %d:", texlength);
  2519.  
  2520.                 x0 = (ScrMaxX - 25 - 8 * strlen(prompt)) / 2;
  2521.                 y0 = (ScrMaxY - 55) / 2;
  2522.  
  2523.                 DrawScreenBox3D( x0, y0, x0 + 25 + 8 * strlen( prompt), y0 + 55);
  2524.                 SetColor( WHITE);
  2525.                 DrawScreenText( x0 + 10, y0 + 8, prompt);
  2526.  
  2527.                 while ( ((key=InputInteger(x0+10, y0+28, &useroffset, 0, texlength))&0x00FF)!=0x000D
  2528.                                         && (key & 0x00FF) != 0x001B)
  2529.                         Beep();
  2530.  
  2531.                 if (UseMouse)
  2532.                         ShowMousePointer();
  2533.         }
  2534.  
  2535.         while (*sdlist != NULL)  /* main processing loop */
  2536.         {
  2537.                 ldef = 0;
  2538.                 sd1 = (*sdlist)->objnum;
  2539.  
  2540.                 if(type_sd == 1) /* throw out all 2nd SideDefs untill a 1st is found */
  2541.                 {
  2542.                         while(LineDefs[ldef].sidedef1!=sd1 && ldef<=NumLineDefs)
  2543.                         {
  2544.                                 ldef++;
  2545.                                 if(LineDefs[ldef].sidedef2 == sd1)
  2546.                                 {
  2547.                                         UnSelectObject(sdlist, (*sdlist)->objnum);
  2548.                                         sd1 = (*sdlist) ->objnum;
  2549.                                         ldef = 0;
  2550.                                         if((*sdlist) == NULL)
  2551.                                                 return;
  2552.                                 }
  2553.                         }
  2554.                 }
  2555.  
  2556.                 if(type_sd == 2) /* throw out all 1st SideDefs untill a 2nd is found */
  2557.                 {
  2558.                         while(LineDefs[ldef].sidedef2!=sd1 && ldef<=NumLineDefs)
  2559.                         {
  2560.                                 ldef++;
  2561.                                 if(LineDefs[ldef].sidedef1 == sd1)
  2562.                                 {
  2563.                                         UnSelectObject(sdlist, (*sdlist)->objnum);
  2564.                                         sd1 = (*sdlist) ->objnum;
  2565.                                         ldef = 0;
  2566.                                         if((*sdlist) == NULL)
  2567.                                                 return;
  2568.                                 }
  2569.                         }
  2570.                 }
  2571.  
  2572.                 if(type_tex == 1) /* do we test for same textures for the SideDef in question?? */
  2573.  
  2574.                 {
  2575.                         ObjectsNeeded( OBJ_SIDEDEFS, 0);
  2576.                         if (strncmp( SideDefs[ (*sdlist)->objnum].tex3, texname,8))
  2577.                         {
  2578.                                 Beep();
  2579.                                 sprintf( errormessage, "No texture for SideDef #%d.", (*sdlist)->objnum);
  2580.                                 Notify( -1, -1, errormessage, NULL);
  2581.                                 return;
  2582.                         }
  2583.                 }
  2584.  
  2585.                 sd1 = (*sdlist)->objnum;
  2586.                 ldef = 0;
  2587.  
  2588.                 ObjectsNeeded(OBJ_LINEDEFS,0);
  2589.  
  2590.                 /* find out which LineDef holds that SideDef */
  2591.                 if(type_sd == 1)
  2592.                 {
  2593.                         while (LineDefs[ ldef].sidedef1 != sd1 && ldef < NumLineDefs)
  2594.                                 ldef++;
  2595.                 }
  2596.                 else
  2597.                 {
  2598.                         while (LineDefs[ ldef].sidedef2 != sd1 && ldef < NumLineDefs)
  2599.                                 ldef++;
  2600.                 }
  2601.  
  2602.                 vert1 = LineDefs[ ldef].start;
  2603.                 /* test for linedef highlight continuity */
  2604.                 if (vert1 != vert2 && vert2 != -1)
  2605.                 {
  2606.                         Beep();
  2607.                         sprintf( errormessage, "LineDef #%d is not contiguous with the previous LineDef, please reselect.", (*sdlist)->objnum);
  2608.                         Notify( -1, -1, errormessage, NULL);
  2609.                         return;
  2610.                 }
  2611.                 /* is this the first time round here */
  2612.                 if(vert1 != vert2)
  2613.                 {
  2614.                         if(type_off == 1)  /* do we have an initial offset ? */
  2615.                         {
  2616.                                 SideDefs[sd1].xoff = useroffset;
  2617.                                 xoffset = useroffset;
  2618.                         }
  2619.                         else
  2620.                                 SideDefs[sd1].xoff = 0;
  2621.                 }
  2622.                 else            /* put new xoffset into the SideDef */
  2623.                         SideDefs[sd1].xoff = xoffset;
  2624.  
  2625.                 /* calculate length of LineDef */
  2626.                 vert2 = LineDefs[ldef].end;
  2627.                 ObjectsNeeded( OBJ_VERTEXES, 0);
  2628.                 length = ComputeDist( Vertexes[vert2].x - Vertexes[vert1].x, Vertexes[vert2].y - Vertexes[vert1].y);
  2629.  
  2630.                 xoffset += length;
  2631.                 /* remove multiples of texlength from xoffset */
  2632.                 xoffset = xoffset % texlength;
  2633.                 /* move to next object in selected list */
  2634.                 UnSelectObject( sdlist, (*sdlist)->objnum);
  2635.         }
  2636.         MadeChanges = TRUE;
  2637. }
  2638.  
  2639.  
  2640.  
  2641. /*
  2642.    Distribute sector floor heights
  2643. */
  2644.  
  2645. void DistributeSectorFloors( SelPtr obj) /* SWAP! */
  2646. {
  2647.    SelPtr cur;
  2648.    int    n, num, floor1h, floor2h;
  2649.  
  2650.    ObjectsNeeded( OBJ_SECTORS, 0);
  2651.  
  2652.    num = 0;
  2653.    for (cur = obj; cur->next; cur = cur->next)
  2654.       num++;
  2655.  
  2656.    floor1h = Sectors[ obj->objnum].floorh;
  2657.    floor2h = Sectors[ cur->objnum].floorh;
  2658.  
  2659.    n = 0;
  2660.    for (cur = obj; cur; cur = cur->next)
  2661.    {
  2662.       Sectors[ cur->objnum].floorh = floor1h + n * (floor2h - floor1h) / num;
  2663.       n++;
  2664.    }
  2665.    MadeChanges = TRUE;
  2666. }
  2667.  
  2668.  
  2669.  
  2670. /*
  2671.    Distribute sector ceiling heights
  2672. */
  2673.  
  2674. void DistributeSectorCeilings( SelPtr obj) /* SWAP! */
  2675. {
  2676.    SelPtr cur;
  2677.    int    n, num, ceil1h, ceil2h;
  2678.  
  2679.    ObjectsNeeded( OBJ_SECTORS, 0);
  2680.  
  2681.    num = 0;
  2682.    for (cur = obj; cur->next; cur = cur->next)
  2683.       num++;
  2684.  
  2685.    ceil1h = Sectors[ obj->objnum].ceilh;
  2686.    ceil2h = Sectors[ cur->objnum].ceilh;
  2687.  
  2688.    n = 0;
  2689.    for (cur = obj; cur; cur = cur->next)
  2690.    {
  2691.       Sectors[ cur->objnum].ceilh = ceil1h + n * (ceil2h - ceil1h) / num;
  2692.       n++;
  2693.    }
  2694.    MadeChanges = TRUE;
  2695. }
  2696.  
  2697.  
  2698.  
  2699. /* end of file */
  2700.