home *** CD-ROM | disk | FTP | other *** search
/ Doom Fever / Doom_Fever-1995_Maple_Media.iso / wad / source.zip / OBJECTS.C < prev    next >
C/C++ Source or Header  |  1994-05-22  |  77KB  |  2,698 lines

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