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

  1. /*
  2.    Amazing Doom Editor, by Brendon Wyber and RaphaĆ«l Quinet.
  3.  
  4.    Doom II code by Adrian Cable.
  5.  
  6.    You are allowed to use any parts of this code in another program, as
  7.    long as you give credits to the authors in the documentation and in
  8.    the program itself.  Read the file README.1ST for more information.
  9.  
  10.    This program comes with absolutely no warranty.
  11.  
  12.    EDIT.C - Editor routines.
  13. */
  14.  
  15. /* the includes */
  16. #include "deu.h"
  17. #include "levels.h"
  18. extern Bool InfoShown;          /* should we display the info bar? */
  19. #ifdef CIRRUS_PATCH
  20. extern char HWCursor[];         /* Cirrus hardware cursor data */
  21. #endif /* CIRRUS_PATCH */
  22.  
  23. int  mapno = 0;
  24.  
  25. extern int convflag;
  26. extern int curlev;
  27.  
  28. /*
  29.    the driving program
  30. */
  31.  
  32. void EditLevel( int episode, int mission, Bool newlevel)
  33. {
  34.    ReadWTextureNames();
  35.    ReadFTextureNames();
  36.    InitGfx();
  37.    CheckMouseDriver();
  38.    if (episode + mission == 0)
  39.    {
  40.       SelectLevel( &episode, &mission);
  41.       curlev = (episode*10)+mission;
  42.    }
  43.    if (episode + mission > 0)
  44.    {
  45.       ClearScreen();
  46.       ReadLevelData( episode, mission);
  47.       if (newlevel)
  48.       {
  49.          ForgetLevelData();
  50.          MapMinX = -4000;
  51.          MapMinY = -4000;
  52.          MapMaxX = 4000;
  53.          MapMaxY = 4000;
  54.       }
  55.       LogMessage( ": Editing MAP%d%d...\n", episode, mission);
  56.       EditorLoop( episode, mission);
  57.       LogMessage( ": Finished editing MAP%d%d...\n", episode, mission);
  58.       TermGfx();
  59.       if ((FindMasterDir( MasterDir, "MAP01") == NULL) && (FindMasterDir( MasterDir, "MAP30") == NULL))
  60.          printf( "Please register DOOM if you want to be able to save your changes.\n");
  61.       ForgetLevelData();
  62.       /* forget the level pointer */
  63.       Level = NULL;
  64.    }
  65.    else
  66.       TermGfx();
  67.    ForgetWTextureNames();
  68.    ForgetFTextureNames();
  69. }
  70.  
  71.  
  72.  
  73. /*
  74.    select a level
  75. */
  76.  
  77. void SelectLevel( int *episode, int *mission)
  78. {
  79.    MDirPtr dir;
  80.    char name[ 7];
  81.    char **levels;
  82.    float mapno;
  83.    int n = 0;
  84.  
  85.    dir = MasterDir;
  86.    while (dir)
  87.    {
  88.       if (dir->dir.size == 0 && dir->dir.name[ 0] == 'M' && dir->dir.name[ 1] == 'A' && dir->dir.name[ 5] == '\0')
  89.       {
  90.          if (n == 0)
  91.             levels = GetMemory( sizeof( char *));
  92.          else
  93.             levels = ResizeMemory( levels, (n + 1) * sizeof( char *));
  94.          levels[ n] = dir->dir.name;
  95.          n++;
  96.       }
  97.       dir = dir->next;
  98.    }
  99.    if (*episode + *mission == 0)
  100.       *episode = 0;
  101.       *mission = 1;
  102.    sprintf( name, "MAP%d%d", *episode, *mission);
  103.    InputNameFromList( -1, -1, "Select an episode and a mission number:", n, levels, name);
  104.    FreeMemory( levels);
  105.    if (*name)
  106.       sscanf( name, "MAP%d%d", episode, mission);
  107.       mapno = (float)*episode;
  108.       *episode = ((int)mapno/10);
  109.       *mission = ((int)mapno%10);
  110. }
  111.  
  112. void SelectTranslatedLevel( int *episode, int *mission, char *entryname)
  113. {
  114.    MDirPtr dir;
  115.    char name[ 7];
  116.    char dialogstring[ 60];
  117.    char **levels;
  118.    float mapno;
  119.    int n = 0;
  120.  
  121.    dir = MasterDir;
  122.    while (dir)
  123.    {
  124.       if (dir->dir.size == 0 && dir->dir.name[ 0] == 'M' && dir->dir.name[ 1] == 'A' && dir->dir.name[ 5] == '\0')
  125.       {
  126.          if (n == 0)
  127.             levels = GetMemory( sizeof( char *));
  128.          else
  129.             levels = ResizeMemory( levels, (n + 1) * sizeof( char *));
  130.          levels[ n] = dir->dir.name;
  131.          n++;
  132.       }
  133.       dir = dir->next;
  134.    }
  135.    if (*episode + *mission == 0)
  136.       *episode = 0;
  137.       *mission = 1;
  138.    sprintf( name, "MAP%d%d", *episode, *mission);
  139. // printf( "Select level to map PWAD's %s onto:", entryname);
  140. // printf( "Select level to map PWAD's %s onto:", *entryname);
  141. // printf( "Select level to map PWAD's %s onto:", &entryname);
  142.    sprintf( dialogstring, "Select level to map the PWAD's %s onto:", entryname);
  143.    InputNameFromList( -1, -1, dialogstring, n, levels, name);
  144.    FreeMemory( levels);
  145.    if (*name)
  146.    {
  147.       sscanf( name, "MAP%d%d", episode, mission);
  148.       mapno = (float)*episode;
  149.       *episode = ((int)mapno/10);
  150.       *mission = ((int)mapno%10);
  151.    }
  152.    if (*episode + *mission == 0)
  153.       *mission = 1;
  154. }
  155.  
  156. void ConvertSidedefs()
  157. {
  158.    int k;
  159.    for (k=0; k<NumSideDefs; k++)
  160.    {
  161.        ConvertTexture( SideDefs[ k].tex1);
  162.        ConvertTexture( SideDefs[ k].tex2);
  163.        ConvertTexture( SideDefs[ k].tex3);
  164.    }
  165. }
  166.  
  167. void ConvertTexture( char *texture)
  168. {
  169.    if (!strncmp( texture, "COMPTILE", 8))
  170.       strncpy( texture, "COMPBLUE", 8);
  171.    if (!strncmp( texture, "AASTINKY", 8))
  172.       strncpy( texture, "ASHWALL2", 8);
  173.    if (!strncmp( texture, "ASHWALL", 7))
  174.       strncpy( texture, "ASHWALL2", 8);
  175.    if (!strncmp( texture, "BLODGR1", 7))
  176.       strncpy( texture, "BROWNHUG", 8);
  177.    if (!strncmp( texture, "BLODGR2", 7))
  178.       strncpy( texture, "BROWNHUG", 8);
  179.    if (!strncmp( texture, "BLODGR3", 7))
  180.       strncpy( texture, "BROWNHUG", 8);
  181.    if (!strncmp( texture, "BLODGR4", 7))
  182.       strncpy( texture, "BROWNHUG", 8);
  183.    if (!strncmp( texture, "BRNBIGC", 7))
  184.       strncpy( texture, "BRNSMALC", 8);
  185.    if (!strncmp( texture, "BRNBIGL", 7))
  186.       strncpy( texture, "BRNSMALL", 8);
  187.    if (!strncmp( texture, "BRNBIGR", 7))
  188.       strncpy( texture, "BRNSMALR", 8);
  189.    if (!strncmp( texture, "BRNPOIS2", 8))
  190.       strncpy( texture, "BRNPOIS\0", 8);
  191.    if (!strncmp( texture, "BROVINE", 7))
  192.       strncpy( texture, "BROVINE2", 8);
  193.    if (!strncmp( texture, "BROWNWEL", 8))
  194.       strncpy( texture, "BROWNHUG", 8);
  195.    if (!strncmp( texture, "CEMPOIS", 7))
  196.       strncpy( texture, "CEMENT6", 7);
  197.    if (!strncmp( texture, "COMP2", 5))
  198.       strncpy( texture, "COMPTALL", 8);
  199.    if (!strncmp( texture, "COMPOHSO", 8))
  200.       strncpy( texture, "COMPBLUE", 8);
  201.    if (!strncmp( texture, "COMPTILE", 8))
  202.       strncpy( texture, "COMPBLUE", 8);
  203.    if (!strncmp( texture, "COMPUTE1", 8))
  204.       strncpy( texture, "COMPSTA1", 8);
  205.    if (!strncmp( texture, "COMPUTE2", 8))
  206.       strncpy( texture, "COMPSTA2", 8);
  207.    if (!strncmp( texture, "COMPUTE3", 8))
  208.       strncpy( texture, "COMPTALL", 8);
  209.    if (!strncmp( texture, "DOORHI", 6))
  210.       strncpy( texture, "DOOR3\0", 6);
  211.    if (!strncmp( texture, "GRAYDANG", 8))
  212.       strncpy( texture, "GRAYBIG\0", 8);
  213.    if (!strncmp( texture, "ICKDOOR1", 8))
  214.       strncpy( texture, "DOOR1\0\0\0", 8);
  215.    if (!strncmp( texture, "ICKWALL6", 8))
  216.       strncpy( texture, "ICKWALL7", 8);
  217.    if (!strncmp( texture, "LITE2", 5))
  218.       strncpy( texture, "LITE3", 5);
  219.    if (!strncmp( texture, "LITE4", 5))
  220.       strncpy( texture, "LITE3", 5);
  221.    if (!strncmp( texture, "LITE96", 6))
  222.       strncpy( texture, "LITE3\0", 6);
  223.    if (!strncmp( texture, "LITEBLU2", 8))
  224.       strncpy( texture, "LITEBLU1", 8);
  225.    if (!strncmp( texture, "LITEBLU3", 8))
  226.       strncpy( texture, "LITEBLU1", 8);
  227.    if (!strncmp( texture, "LITEBLU4", 8))
  228.       strncpy( texture, "LITE5\0\0\0", 8);
  229.    if (!strncmp( texture, "LITEMET", 7))
  230.       strncpy( texture, "LITE5\0\0", 7);
  231.    if (!strncmp( texture, "LITERED", 7))
  232.       strncpy( texture, "LITE5\0\0", 7);
  233.    if (!strncmp( texture, "LITESTON", 8))
  234.       strncpy( texture, "LITE5\0\0\0", 8);
  235.    if (!strncmp( texture, "MIDVINE1", 8))
  236.       strncpy( texture, "MIDGRATE", 8);
  237.    if (!strncmp( texture, "MIDVINE2", 8))
  238.       strncpy( texture, "MIDGRATE", 8);
  239.    if (!strncmp( texture, "NUKESLAD", 8))
  240.       strncpy( texture, "NUKEPOIS", 8);
  241.    if (!strncmp( texture, "PLANET1", 7))
  242.       strncpy( texture, "COMPSTA1", 8);
  243.    if (!strncmp( texture, "REDWALL1", 8))
  244.       strncpy( texture, "REDWALL\0", 8);
  245.    if (!strncmp( texture, "SKINBORD", 8))
  246.       strncpy( texture, "SKINCUT\0", 8);
  247.    if (!strncmp( texture, "SKINTEK1", 8))
  248.       strncpy( texture, "SKINSCAB", 8);
  249.    if (!strncmp( texture, "SKINTEK2", 8))
  250.       strncpy( texture, "SKINSYMB", 8);
  251.    if (!strncmp( texture, "SKULWAL3", 8))
  252.       strncpy( texture, "SKSPINE1", 8);
  253.    if (!strncmp( texture, "SKULWALL", 8))
  254.       strncpy( texture, "SKSPINE1", 8);
  255.    if (!strncmp( texture, "SLADRIP1", 8))
  256.       strncpy( texture, "SLADPOIS", 8);
  257.    if (!strncmp( texture, "SLADRIP2", 8))
  258.       strncpy( texture, "SLADPOIS", 8);
  259.    if (!strncmp( texture, "SLADRIP3", 8))
  260.       strncpy( texture, "SLADPOIS", 8);
  261.    if (!strncmp( texture, "STARTAN1", 8))
  262.       strncpy( texture, "STARTAN2", 8);
  263.    if (!strncmp( texture, "STONGARG", 8))
  264.       strncpy( texture, "STONE5\0\0", 8);
  265.    if (!strncmp( texture, "STONPOIS", 8))
  266.       strncpy( texture, "SLADPOIS", 8);
  267.    if (!strncmp( texture, "SP_DUDE3", 8))
  268.       strncpy( texture, "SP_DUDE8", 8);
  269.    if (!strncmp( texture, "SP_DUDE6", 8))
  270.       strncpy( texture, "SP_DUDE5", 8);
  271.    if (!strncmp( texture, "SP_ROCK2", 8))
  272.       strncpy( texture, "SP_ROCK1", 8);
  273.    if (!strncmp( texture, "TEKWALL1", 8))
  274.       strncpy( texture, "TEKGREN1", 8);
  275.    if (!strncmp( texture, "TEKWALL2", 8))
  276.       strncpy( texture, "TEKGREN2", 8);
  277.    if (!strncmp( texture, "TEKWALL3", 8))
  278.       strncpy( texture, "TEKGREN3", 8);
  279.    if (!strncmp( texture, "TEKWALL4", 8))
  280.       strncpy( texture, "TEKGREN4", 8);
  281.    if (!strncmp( texture, "TEKWALL5", 8))
  282.       strncpy( texture, "TEKGREN5", 8);
  283.    if (!strncmp( texture, "WOODSKUL", 8))
  284.       strncpy( texture, "WOODGARG", 8);
  285. }
  286.  
  287. /*
  288.    get the name of the new WAD file
  289. */
  290.  
  291. char *GetWadFileName( int episode, int mission)
  292. {
  293.    char *outfile = GetMemory( 80);
  294.    char *dotp;
  295.    WadPtr wad;
  296.  
  297.    /* get the file name */
  298.    if (! strcmp(Level->wadfile->filename, MainWad))
  299.       sprintf( outfile, "MAP%d%d.WAD", episode, mission);
  300.    else
  301.       strcpy( outfile, Level->wadfile->filename);
  302.    do
  303.    {
  304.       InputFileName( -1, -1, "Name of the new WAD file:", 79, outfile);
  305.    }
  306.    while (!strcmp(outfile, MainWad));
  307.    /* escape */
  308.    if (outfile[ 0] == '\0')
  309.    {
  310.       FreeMemory( outfile);
  311.       return NULL;
  312.    }
  313.    /* if the WAD file already exists, rename it to "*.BAK" */
  314.    for (wad = WadFileList; wad; wad = wad->next)
  315.       if (!stricmp( outfile, wad->filename))
  316.       {
  317.          dotp = strrchr( wad->filename, '.');
  318.          if (dotp == NULL)
  319.             strcat( wad->filename, ".BAK");
  320.          else
  321.             strcpy( dotp, ".BAK");
  322.          /* need to close, then reopen: problems with SHARE.EXE */
  323.          fclose( wad->fileinfo);
  324.          if (rename( outfile, wad->filename) < 0)
  325.          {
  326.             if (unlink( wad->filename) < 0)
  327.                ProgError( "could not delete file \"%s\"", wad->filename);
  328.             if (rename( outfile, wad->filename) < 0)
  329.                ProgError( "could not rename \"%s\" to \"%s\"", outfile, wad->filename);
  330.          }
  331.          wad->fileinfo = fopen( wad->filename, "rb");
  332.          if (wad->fileinfo == NULL)
  333.             ProgError( "could not reopen file \"%s\"", wad->filename);
  334.          break;
  335.       }
  336.    return outfile;
  337. }
  338.  
  339.  
  340.  
  341. /*
  342.    display the help screen
  343.  */
  344.  
  345. void DisplayHelp( int objtype, int grid) /* SWAP! */
  346. {
  347.    int x0 = 137;
  348.    int y0 = 50;
  349.  
  350.    if (UseMouse)
  351.       HideMousePointer();
  352.    /* put in the instructions */
  353.    DrawScreenBox3D( x0, y0, x0 + 364, y0 + 355);
  354.    SetColor( LIGHTCYAN);
  355.    DrawScreenText( x0 + 100, y0 + 20, "Amazing Doom Editor");
  356.    DrawScreenText( 269 - strlen(GetEditModeName( objtype)) * 4, y0 + 32, "- %s Editor -", GetEditModeName( objtype));
  357.    SetColor( BLACK);
  358.    DrawScreenText( x0 + 10, y0 + 60, "Use the mouse or the cursor keys to move");
  359.    DrawScreenText( -1, -1, "around.  The map scrolls when the pointer");
  360.    DrawScreenText( -1, -1, "reaches the edge of the screen.");
  361.    DrawScreenText( -1, y0 + 100, "Other useful keys are:");
  362.    if ((FindMasterDir( MasterDir, "RROCK04") != NULL) && (FindMasterDir( MasterDir, "MAP12") != NULL))
  363.       DrawScreenText( -1, y0 + 115, "Q     - Quit, saving changes");
  364.    else
  365.    {
  366.       SetColor( DARKGRAY);
  367.       DrawScreenText( -1, y0 + 115, "Q     - Quit without saving changes");
  368.       SetColor( BLACK);
  369.    }
  370.    DrawScreenText( -1, -1, "Esc   - Exit without saving changes");
  371.    DrawScreenText( -1, -1, "Tab   - Switch to the next editing mode");
  372.    DrawScreenText( -1, -1, "Space - Change the move/scroll speed");
  373.    DrawScreenText( -1, -1, "+/-   - Change the map scale (current: %d)", (int) (1.0 / Scale + 0.5));
  374.    DrawScreenText( -1, -1, "G     - Change the grid scale (cur.: %d)", grid);
  375.    DrawScreenText( -1, -1, "N, >  - Jump to the next object.");
  376.    DrawScreenText( -1, -1, "P, <  - Jump to the previous object.");
  377.    DrawScreenText( -1, -1, "J, #  - Jump to a specific object (enter #)");
  378.    DrawScreenText( -1, -1, "M     - Mark/unmark current object (select)");
  379.    if (objtype == OBJ_THINGS || objtype == OBJ_VERTEXES)
  380.       DrawScreenText( -1, -1, "D     - Toggle drag mode");
  381.    else
  382.       DrawScreenText( -1, -1, "C     - Clear all marks and redraw map");
  383.    DrawScreenText( -1, -1, "Ins   - Insert a new object");
  384.    DrawScreenText( -1, -1, "Del   - Delete the current object");
  385.    DrawScreenText( -1, -1, "Enter - Edit the current/selected object(s)");
  386.    DrawScreenText( -1, y0 + 265, "Mouse buttons:");
  387.    if (SwapButtons)
  388.    {
  389.       DrawScreenText( -1, y0 + 280, "Left  - Edit the current/selected object(s)");
  390.       DrawScreenText( -1, -1, "Middle- Mark/unmark the current object.");
  391.    }
  392.    else
  393.    {
  394.       DrawScreenText( -1, y0 + 280, "Left  - Mark/unmark the current object");
  395.       DrawScreenText( -1, -1, "Middle- Edit the current/selected object(s)");
  396.    }
  397.    DrawScreenText( -1, -1, "Right - Drag the current/selected object(s)");
  398.    DrawScreenText( -1, y0 + 320, "Please read DEU.TXT for more information");
  399.    SetColor( YELLOW);
  400.    DrawScreenText( -1, y0 + 340, "Press any key to return to the editor...");
  401.    bioskey( 0);
  402.    if (UseMouse)
  403.       ShowMousePointer();
  404. }
  405.  
  406. /*
  407.    the editor main loop
  408. */
  409.  
  410. void EditorLoop( int episode, int mission) /* SWAP! */
  411. {
  412.    int    EditMode = OBJ_THINGS;
  413.    int    CurObject = -1;
  414.    int    OldObject = -1;
  415.    Bool   RedrawMap = TRUE;
  416.    Bool   RedrawObj = FALSE;
  417.    Bool   DragObject = FALSE;
  418.    int    key, altkey, buttons, oldbuttons;
  419.    int    GridScale = 0;
  420.    Bool   GridShown = TRUE;
  421.    SelPtr Selected = NULL;
  422.    char   keychar;
  423.    int    SelBoxX = 0;
  424.    int    SelBoxY = 0;
  425.    int    OldPointerX = 0;
  426.    int    OldPointerY = 0;
  427.    Bool   StretchSelBox = FALSE;
  428.    Bool   ShowRulers = FALSE;
  429.    char   *outfile;
  430.  
  431.    MadeChanges = FALSE;
  432.    MadeMapChanges = FALSE;
  433.    if (convflag == 1)
  434.    {
  435.       convflag = 0;
  436.       ConvertSidedefs();
  437.       ClearScreen();
  438.       outfile = GetTemporary();
  439.       if (outfile)
  440.       {
  441.          SaveLevelData( outfile);
  442.          return;
  443.       }
  444.       else
  445.          return;
  446.    }
  447.    if (InitialScale < 1)
  448.       InitialScale = 1;
  449.    else if (InitialScale > 20)
  450.       InitialScale = 20;
  451.    Scale = (float) (1.0 / InitialScale);
  452.    CenterMapAroundCoords( (MapMinX + MapMaxX) / 2, (MapMinY + MapMaxY) / 2);
  453.    if (UseMouse)
  454.    {
  455.       ResetMouseLimits();
  456.       SetMouseCoords( PointerX, PointerY);
  457.       ShowMousePointer();
  458. #ifdef CIRRUS_PATCH
  459.       if (CirrusCursor == TRUE)
  460.       {
  461.          SetHWCursorMap( HWCursor);
  462.          SetHWCursorPos( PointerX, PointerY);
  463.       }
  464. #endif /* CIRRUS_PATCH */
  465.       oldbuttons = 0;
  466.    }
  467.    else
  468.       FakeCursor = TRUE;
  469.  
  470.    for (;;)
  471.    {
  472.       key = 0;
  473.       altkey = 0;
  474.  
  475.       /* get mouse position and button status */
  476.       if (UseMouse)
  477.       {
  478.          if (FakeCursor || ShowRulers)
  479.          {
  480.             HideMousePointer();
  481.             DrawPointer( ShowRulers);
  482.             ShowMousePointer();
  483.          }
  484.          GetMouseCoords( &PointerX, &PointerY, &buttons);
  485.          if (FakeCursor || ShowRulers)
  486.          {
  487.             HideMousePointer();
  488.             DrawPointer( ShowRulers);
  489.             ShowMousePointer();
  490.          }
  491.          if ( buttons == 1 && PointerY < 17)
  492.          {
  493.             /* kluge for the menu bar */
  494.             altkey = 0x08;
  495.             if (PointerX < 12)
  496.                Beep();
  497.             else if (PointerX < 60)
  498.                key = 0x2100; /* 'F' */
  499.             else if (PointerX < 108)
  500.                key = 0x1200; /* 'E' */
  501.             else if (PointerX < 172)
  502.                key = 0x1f00; /* 'S' */
  503.             else if (PointerX < 228)
  504.                key = 0x3200; /* 'M' */
  505.             else if (PointerX < 276)
  506.                key = 0x1700; /* 'I' */
  507.             else if (PointerX < 348)
  508.                key = 0x1800; /* 'O' */
  509.             else if (PointerX < 406)
  510.                key = 0x2E00; /* 'C' */
  511.             else if (PointerX < ScrMaxX - 43)
  512.                Beep();
  513.             else
  514.                key = 0x2300; /* 'H' */
  515.          }
  516.          else
  517.          {
  518.             if (buttons != oldbuttons)
  519.             {
  520.                switch (buttons)
  521.                {
  522.                case 1:
  523.                   if (SwapButtons)
  524.                      key = 0x000D;
  525.                   else
  526.                      key = 'M';      /* Press left button = Mark/Unmark ('M') */
  527.                   break;
  528.                case 2:
  529.                   if (! DragObject)
  530.                      key = 'D';      /* Press right button = Drag */
  531.                   break;
  532.                case 3:
  533.                case 4:
  534.                   if (SwapButtons)
  535.                      key = 'M';
  536.                   else
  537.                      key = 0x000D;   /* Press middle button = Edit ('Enter') */
  538.                   break;
  539.                default:
  540.                   if (StretchSelBox) /* Release left button = End Selection Box */
  541.                      key = 'M';
  542.                   if (DragObject)    /* Release right button = End Drag */
  543.                      key = 'D';
  544.                   break;
  545.                }
  546.                altkey = bioskey( 2);
  547.             }
  548.          }
  549.          oldbuttons = buttons;
  550.       }
  551.  
  552.       /* drag object(s) */
  553.       if (DragObject)
  554.       {
  555.          int forgetit = FALSE;
  556.  
  557.          if (IsSelected( Selected, CurObject) == FALSE)
  558.             ForgetSelection( &Selected);
  559.          else if (Selected->objnum != CurObject)
  560.          {
  561.             /* current object must be first in the list */
  562.             UnSelectObject( &Selected, CurObject);
  563.             SelectObject( &Selected, CurObject);
  564.          }
  565.          if (Selected == NULL && CurObject >= 0)
  566.          {
  567.             SelectObject( &Selected, CurObject);
  568.             forgetit = TRUE;
  569.          }
  570.          if (Selected)
  571.          {
  572.             if (MoveObjectsToCoords( EditMode, Selected, MAPX( PointerX), MAPY( PointerY), GridScale))
  573.                RedrawMap = TRUE;
  574.             if (forgetit)
  575.                ForgetSelection( &Selected);
  576.          }
  577.          else
  578.          {
  579.             Beep();
  580.             DragObject = FALSE;
  581.          }
  582.       }
  583.       else if (StretchSelBox)
  584.       {
  585.          int x = MAPX( PointerX);
  586.          int y = MAPY( PointerY);
  587.  
  588.          /* draw selection box */
  589.          SetColor( CYAN);
  590.          setwritemode( XOR_PUT);
  591.          if (UseMouse)
  592.             HideMousePointer();
  593.          DrawMapLine( SelBoxX, SelBoxY, SelBoxX, y);
  594.          DrawMapLine( SelBoxX, y, x, y);
  595.          DrawMapLine( x, y, x, SelBoxY);
  596.          DrawMapLine( x, SelBoxY, SelBoxX, SelBoxY);
  597.          if (UseMouse)
  598.             ShowMousePointer();
  599.          delay( 50);
  600.          if (UseMouse)
  601.             HideMousePointer();
  602.          DrawMapLine( SelBoxX, SelBoxY, SelBoxX, y);
  603.          DrawMapLine( SelBoxX, y, x, y);
  604.          DrawMapLine( x, y, x, SelBoxY);
  605.          DrawMapLine( x, SelBoxY, SelBoxX, SelBoxY);
  606.          setwritemode( COPY_PUT);
  607.          if (UseMouse)
  608.             ShowMousePointer();
  609.       }
  610.       else if (!RedrawObj)
  611.       {
  612.          /* check if there is something near the pointer */
  613.          OldObject = CurObject;
  614.          if ((bioskey( 2) & 0x03) == 0x00)  /* no shift keys */
  615.             CurObject = GetCurObject( EditMode, MAPX( PointerX - 4), MAPY( PointerY - 4), MAPX( PointerX + 4), MAPY( PointerY + 4));
  616.          if (CurObject < 0)
  617.             CurObject = OldObject;
  618.       }
  619.  
  620.       /* draw the map */
  621.       if (RedrawMap)
  622.       {
  623.          if (UseMouse)
  624.             HideMousePointer();
  625.          DrawMap( EditMode, GridScale, GridShown);
  626.          HighlightSelection( EditMode, Selected);
  627.          if (UseMouse)
  628.             ShowMousePointer();
  629.       }
  630.  
  631.       /* highlight the current object and display the information box */
  632.       if (RedrawMap || CurObject != OldObject || RedrawObj)
  633.       {
  634.          RedrawObj = FALSE;
  635.          if (UseMouse)
  636.             HideMousePointer();
  637.          if (!RedrawMap && OldObject >= 0)
  638.             HighlightObject( EditMode, OldObject, YELLOW);
  639.          if (CurObject != OldObject)
  640.          {
  641.             PlaySound( 50, 10);
  642.             OldObject = CurObject;
  643.          }
  644.          if (CurObject >= 0)
  645.             HighlightObject( EditMode, CurObject, YELLOW);
  646.          if (bioskey( 1)) /* speedup */
  647.             RedrawObj = TRUE;
  648.          else
  649.             DisplayObjectInfo( EditMode, CurObject);
  650.          if (UseMouse)
  651.             ShowMousePointer();
  652.       }
  653.  
  654.       /* redraw the pointer if necessary */
  655.       if (RedrawMap && (FakeCursor || ShowRulers))
  656.       {
  657.          if (UseMouse)
  658.             HideMousePointer();
  659.          DrawPointer( ShowRulers);
  660.          if (UseMouse)
  661.             ShowMousePointer();
  662.       }
  663.  
  664.       /* display the current pointer coordinates */
  665.       if (RedrawMap || PointerX != OldPointerX || PointerY != OldPointerY)
  666.       {
  667.          SetColor( LIGHTGRAY);
  668.          DrawScreenBox( ScrMaxX - 170, 4, ScrMaxX - 50, 12);
  669.          SetColor( BLUE);
  670.          DrawScreenText( ScrMaxX - 170, 4, "%d, %d", MAPX( PointerX), MAPY( PointerY));
  671.          OldPointerX = PointerX;
  672.          OldPointerY = PointerY;
  673.       }
  674.  
  675.       /* the map is up to date */
  676.       RedrawMap = FALSE;
  677.  
  678.       /* get user input */
  679.       if (bioskey( 1) || key)
  680.       {
  681.          if (! key)
  682.          {
  683.             key = bioskey( 0);
  684.             altkey = bioskey( 2);
  685.          }
  686.  
  687.          /* user wants to access the drop-down menus */
  688.          if (altkey & 0x08)    /* if alt is pressed... */
  689.          {
  690.             if ((key & 0xFF00) == 0x2100)       /* Scan code for F */
  691.                key = PullDownMenu( 18, 19,
  692.                                    "Save          F2", 0x3C00,    (int) 'S', 1,
  693.                                    "Save As MAPxy F3", 0x3D00,    (int) 'A', 6,
  694.                                    "Print           ", -1,        (int) 'P', -1,
  695.                                    "Quit           Q", (int) 'Q', (int) 'Q', 1,
  696.                                    NULL);
  697.             else if ((key & 0xFF00) == 0x1200)  /* Scan code for E */
  698.             {
  699.                key = PullDownMenu( 66, 19,
  700.                                    "Copy object(s)      O", (int) 'O', (int) 'C', 1,
  701.                                    "Add object        Ins", 0x5200,    (int) 'A', 1,
  702.                                    "Delete object(s)  Del", 0x5300,    (int) 'D', 1,
  703.                                    ((EditMode == OBJ_VERTEXES) ?
  704.                                    NULL :
  705.                                    "Preferences        F5"), 0x3F00,   (int) 'P', 1,
  706.                                    NULL);
  707.             }
  708.             else if ((key & 0xFF00) == 0x1F00)  /* Scan code for S */
  709.                key = PullDownMenu( 114, 19,
  710.                                    "Find/Change       F4", -1,        (int) 'F', -1,
  711.                                    "Repeat last find    ", -1,        (int) 'R', -1,
  712.                                    "Next object        N", (int) 'N', (int) 'N', 1,
  713.                                    "Prev object        P", (int) 'P', (int) 'P', 1,
  714.                                    "Jump to object...  J", (int) 'J', (int) 'J', 1,
  715.                                    NULL);
  716.             else if ((key & 0xFF00) == 0x3200)  /* Scan code for M */
  717.                key = PullDownMenu( 178, 19,
  718.                                    ((EditMode == OBJ_THINGS) ?
  719.                                    "√ Things              T" :
  720.                                    "  Things              T"), (int) 'T', (int) 'T', 3,
  721.                                    ((EditMode == OBJ_LINEDEFS) ?
  722.                                    "√ Linedefs+Sidedefs   L" :
  723.                                    "  Linedefs+Sidedefs   L"), (int) 'L', (int) 'L', 3,
  724.                                    ((EditMode == OBJ_VERTEXES) ?
  725.                                    "√ Vertexes            V" :
  726.                                    "  Vertexes            V"), (int) 'V', (int) 'V', 3,
  727.                                    ((EditMode == OBJ_SECTORS) ?
  728.                                    "√ Sectors             S" :
  729.                                    "  Sectors             S"), (int) 'S', (int) 'S', 3,
  730.                                    "  Next mode         Tab",  0x0009,    (int) 'N', 3,
  731.                                    "  Last mode   Shift+Tab",  0x0F00,    (int) 'L', 3,
  732.                                    "  3D Preview          3",  (int) '3', (int) '3', -1,
  733.                                    NULL);
  734.             else if ((key & 0xFF00) == 0x1700)  /* Scan code for I */
  735.             {
  736.                key = 0;
  737.                /* code duplicated from 'F8' - I hate to do that */
  738.                if (Selected)
  739.                   MiscOperations( 234, 19, EditMode, &Selected);
  740.                else
  741.                {
  742.                   if (CurObject >= 0)
  743.                      SelectObject( &Selected, CurObject);
  744.                   MiscOperations( 234, 19, EditMode, &Selected);
  745.                   if (CurObject >= 0)
  746.                      UnSelectObject( &Selected, CurObject);
  747.                }
  748.                CurObject = -1;
  749.                DragObject = FALSE;
  750.                StretchSelBox = FALSE;
  751.             }
  752.             else if ((key & 0xFF00) == 0x1800)  /* Scan code for O */
  753.             {
  754.                int savednum, i;
  755.  
  756.                key = 0;
  757.                /* don't want to create the object behind the menu bar... */
  758.                if (PointerY < 20)
  759.                {
  760.                   PointerX = ScrCenterX;
  761.                   PointerY = ScrCenterY;
  762.                }
  763.                /* code duplicated from 'F9' - I hate to do that */
  764.                savednum = NumLineDefs;
  765.                InsertStandardObject( 282, 19, MAPX( PointerX), MAPY( PointerY));
  766.                if (NumLineDefs > savednum)
  767.                {
  768.                   ForgetSelection( &Selected);
  769.                   EditMode = OBJ_LINEDEFS;
  770.                   for (i = savednum; i < NumLineDefs; i++)
  771.                      SelectObject( &Selected, i);
  772.                   CurObject = NumLineDefs - 1;
  773.                   OldObject = -1;
  774.                   DragObject = FALSE;
  775.                   StretchSelBox = FALSE;
  776.                }
  777.             }
  778.             else if ((key & 0xFF00) == 0x2E00)  /* Scan code for C */
  779.             {
  780.                key = 0;
  781.                CheckLevel( 354, 19);
  782.             }
  783.             else if ((key & 0xFF00) == 0x2300)  /* Scan code for H */
  784.                key = PullDownMenu( ScrMaxX, 19,
  785.                                    "  Keyboard & mouse  F1",  0x3B00,    (int) 'K', 3,
  786.                                    (InfoShown ?
  787.                                    "√ Info bar           I" :
  788.                                    "  Info bar           I"), (int) 'I', (int) 'I', 3,
  789.                                    "  About DEU...        ",  -1,        (int) 'A', -1,
  790.                                    NULL);
  791.             else
  792.             {
  793.                Beep();
  794.                key = 0;
  795.             }
  796.             RedrawMap = TRUE;
  797.          }
  798.  
  799.          /* User wants to do the impossible. */
  800.          if (key == -1)
  801.          {
  802.             NotImplemented();
  803.             RedrawMap = TRUE;
  804.          }
  805.  
  806.          /* simplify the checks later on */
  807.          if (isprint(key & 0x00ff))
  808.             keychar = toupper(key);
  809.          else
  810.             keychar = '\0';
  811.  
  812.          /* erase the (keyboard) pointer */
  813.          if (FakeCursor || ShowRulers)
  814.          {
  815.             HideMousePointer();
  816.             DrawPointer( ShowRulers);
  817.             ShowMousePointer();
  818.          }
  819.  
  820.          /* user wants to exit */
  821.          if (keychar == 'Q')
  822.          {
  823.             ForgetSelection( &Selected);
  824.             if (CheckStartingPos())
  825.             {
  826.                if (Registered && MadeChanges)
  827.                {
  828.                   char *outfile;
  829.  
  830.                   outfile = GetWadFileName( episode, mission);
  831.                   if (outfile)
  832.                   {
  833.                      SaveLevelData( outfile);
  834.                      break;
  835.                   }
  836.                }
  837.                else
  838.                   break;
  839.             }
  840.             RedrawMap = TRUE;
  841.          }
  842.          else if ((key & 0x00FF) == 0x001B) /* 'Esc' */
  843.          {
  844.             if (DragObject)
  845.                DragObject = FALSE;
  846.             else if (StretchSelBox)
  847.                StretchSelBox = FALSE;
  848.             else
  849.             {
  850.                ForgetSelection( &Selected);
  851.                if (!MadeChanges || Confirm(-1, -1, "You have unsaved changes.  Do you really want to quit?", NULL))
  852.                   break;
  853.                RedrawMap = TRUE;
  854.             }
  855.          }
  856.  
  857.          /* user is lost */
  858.          else if ((key & 0xFF00) == 0x3B00) /* 'F1' */
  859.          {
  860.             DisplayHelp( EditMode, GridScale);
  861.             RedrawMap = TRUE;
  862.          }
  863.  
  864.          /* user wants to save the level data */
  865.          else if ((key & 0xFF00) == 0x3C00 && Registered) /* 'F2' */
  866.          {
  867.             char *outfile;
  868.  
  869.             if (CheckStartingPos())
  870.             {
  871.                outfile = GetWadFileName( episode, mission);
  872.                if (outfile)
  873.                   SaveLevelData( outfile);
  874.             }
  875.             RedrawMap = TRUE;
  876.          }
  877.  
  878.          /* user wants to save and change the episode and mission numbers */
  879.          else if ((key & 0xFF00) == 0x3D00 && Registered) /* 'F3' */
  880.          {
  881.             char *outfile;
  882.             int   e, m;
  883.             MDirPtr newLevel, oldl, newl;
  884.             char name[ 7];
  885.  
  886.             if (CheckStartingPos())
  887.             {
  888.                outfile = GetWadFileName( episode, mission);
  889.                if (outfile)
  890.                {
  891.                   e = episode;
  892.                   m = mission;
  893.                   SelectLevel( &e, &m);
  894. //                if (e > 0 && m > 0 && (e != episode || m != mission))
  895. //                if (e != episode || m != mission)
  896.                   {
  897.                      /* horrible but it works... */
  898.                      episode = e;
  899.                      mission = m;
  900.                      sprintf( name, "MAP%d%d", episode, mission);
  901.                      newLevel = FindMasterDir( MasterDir, name);
  902.                      oldl = Level;
  903.                      newl = newLevel;
  904.                      for (m = 0; m < 11; m++)
  905.                      {
  906.                         newl->wadfile = oldl->wadfile;
  907.                         if (m > 0)
  908.                            newl->dir = oldl->dir;
  909.                         /*
  910.                         if (!strcmp( outfile, oldl->wadfile->filename))
  911.                         {
  912.                            oldl->wadfile = WadFileList;
  913.                            oldl->dir = lost...
  914.                         }
  915.                         */
  916.                         oldl = oldl->next;
  917.                         newl = newl->next;
  918.                      }
  919.                      Level = newLevel;
  920.                   }
  921.                   SaveLevelData( outfile);
  922.                }
  923.             }
  924.             RedrawMap = TRUE;
  925.          }
  926.  
  927.          /* user wants to get the 'Preferences' menu */
  928.          else if ((key & 0xFF00) == 0x3F00) /* 'F5' */
  929.          {
  930.             Preferences( -1, -1);
  931.             RedrawMap = TRUE;
  932.          }
  933.          /* user wants to get the menu of misc. ops */
  934.          else if ((key & 0xFF00) == 0x4200) /* 'F8' */
  935.          {
  936.             if (Selected)
  937.                MiscOperations( -1, -1, EditMode, &Selected);
  938.             else
  939.             {
  940.                if (CurObject >= 0)
  941.                   SelectObject( &Selected, CurObject);
  942.                MiscOperations( -1, -1, EditMode, &Selected);
  943.                if (CurObject >= 0)
  944.                   UnSelectObject( &Selected, CurObject);
  945.             }
  946.             CurObject = -1;
  947.             RedrawMap = TRUE;
  948.             DragObject = FALSE;
  949.             StretchSelBox = FALSE;
  950.          }
  951.  
  952.          /* user wants to insert a standard shape */
  953.          else if ((key & 0xFF00) == 0x4300) /* 'F9' */
  954.          {
  955.             int savednum, i;
  956.  
  957.             savednum = NumLineDefs;
  958.             InsertStandardObject( -1, -1, MAPX( PointerX), MAPY( PointerY));
  959.             if (NumLineDefs > savednum)
  960.             {
  961.                ForgetSelection( &Selected);
  962.                EditMode = OBJ_LINEDEFS;
  963.                for (i = savednum; i < NumLineDefs; i++)
  964.                   SelectObject( &Selected, i);
  965.                CurObject = NumLineDefs - 1;
  966.                OldObject = -1;
  967.                DragObject = FALSE;
  968.                StretchSelBox = FALSE;
  969.             }
  970.             RedrawMap = TRUE;
  971.          }
  972.  
  973.          /* user wants to check his level */
  974.          else if ((key & 0xFF00) == 0x4400) /* 'F10' */
  975.          {
  976.             CheckLevel( -1, -1);
  977.             RedrawMap = TRUE;
  978.          }
  979.  
  980.          /* user wants to display/hide the info box */
  981.          else if (keychar == 'I')
  982.          {
  983.             InfoShown = !InfoShown;
  984.             RedrawMap = TRUE;
  985.          }
  986.  
  987.          /* user wants to change the scale */
  988.          else if ((keychar == '+' || keychar == '=') && Scale < 4.0)
  989.          {
  990.             OrigX += (int) ((PointerX - ScrCenterX) / Scale);
  991.             OrigY += (int) ((ScrCenterY - PointerY) / Scale);
  992.             if (Scale < 1.0)
  993.                Scale = 1.0 / ((1.0 / Scale) - 1.0);
  994.             else
  995.                Scale = Scale * 2.0;
  996.             OrigX -= (int) ((PointerX - ScrCenterX) / Scale);
  997.             OrigY -= (int) ((ScrCenterY - PointerY) / Scale);
  998.             RedrawMap = TRUE;
  999.          }
  1000.          else if ((keychar == '-' || keychar == '_') && Scale > 0.05)
  1001.          {
  1002.             OrigX += (int) ((PointerX - ScrCenterX) / Scale);
  1003.             OrigY += (int) ((ScrCenterY - PointerY) / Scale);
  1004.             if (Scale < 1.0)
  1005.                Scale = 1.0 / ((1.0 / Scale) + 1.0);
  1006.             else
  1007.                Scale = Scale / 2.0;
  1008.             OrigX -= (int) ((PointerX - ScrCenterX) / Scale);
  1009.             OrigY -= (int) ((ScrCenterY - PointerY) / Scale);
  1010.             RedrawMap = TRUE;
  1011.          }
  1012.  
  1013.          /* user wants to set the scale directly */
  1014.          else if (keychar >= '0' && keychar <= '9')
  1015.          {
  1016.             OrigX += (int) ((PointerX - ScrCenterX) / Scale);
  1017.             OrigY += (int) ((ScrCenterY - PointerY) / Scale);
  1018.             if (keychar == '0')
  1019.                Scale = 0.1;
  1020.             else
  1021.                Scale = 1.0 / (keychar - '0');
  1022.             OrigX -= (int) ((PointerX - ScrCenterX) / Scale);
  1023.             OrigY -= (int) ((ScrCenterY - PointerY) / Scale);
  1024.             RedrawMap = TRUE;
  1025.          }
  1026.  
  1027.          /* user wants to move */
  1028.          else if ((key & 0xFF00) == 0x4800 && (PointerY - MoveSpeed) >= 0)
  1029.          {
  1030.             if (UseMouse)
  1031.                SetMouseCoords( PointerX, PointerY - MoveSpeed);
  1032.             else
  1033.                PointerY -= MoveSpeed;
  1034.          }
  1035.          else if ((key & 0xFF00) == 0x5000 && (PointerY + MoveSpeed) <= ScrMaxY)
  1036.          {
  1037.             if (UseMouse)
  1038.                SetMouseCoords( PointerX, PointerY + MoveSpeed);
  1039.             else
  1040.                PointerY += MoveSpeed;
  1041.          }
  1042.          else if ((key & 0xFF00) == 0x4B00 && (PointerX - MoveSpeed) >= 0)
  1043.          {
  1044.             if (UseMouse)
  1045.                SetMouseCoords( PointerX - MoveSpeed, PointerY);
  1046.             else
  1047.                PointerX -= MoveSpeed;
  1048.          }
  1049.          else if ((key & 0xFF00) == 0x4D00 && (PointerX + MoveSpeed) <= ScrMaxX)
  1050.          {
  1051.             if (UseMouse)
  1052.                SetMouseCoords( PointerX + MoveSpeed, PointerY);
  1053.             else
  1054.                PointerX += MoveSpeed;
  1055.          }
  1056.  
  1057.          /* user wants to scroll the map (scroll one half page at a time) */
  1058.          else if ((key & 0xFF00) == 0x4900 && MAPY( ScrCenterY) < MapMaxY)
  1059.          {
  1060.             OrigY += (int) (ScrCenterY / Scale);
  1061.             RedrawMap = TRUE;
  1062.          }
  1063.          else if ((key & 0xFF00) == 0x5100 && MAPY( ScrCenterY) > MapMinY)
  1064.          {
  1065.             OrigY -= (int) (ScrCenterY / Scale);
  1066.             RedrawMap = TRUE;
  1067.          }
  1068.          else if ((key & 0xFF00) == 0x4700 && MAPX( ScrCenterX) > MapMinX)
  1069.          {
  1070.             OrigX -= (int) (ScrCenterX / Scale);
  1071.             RedrawMap = TRUE;
  1072.          }
  1073.          else if ((key & 0xFF00) == 0x4F00 && MAPX( ScrCenterX) < MapMaxX)
  1074.          {
  1075.             OrigX += (int) (ScrCenterX / Scale);
  1076.             RedrawMap = TRUE;
  1077.          }
  1078.  
  1079.          /* user wants to change the movement speed */
  1080.          else if (keychar == ' ')
  1081.             MoveSpeed = MoveSpeed == 1 ? 20 : 1;
  1082.  
  1083.          /* user wants to change the edit mode */
  1084.          else if ((key & 0x00FF) == 0x0009 || (key & 0xFF00) == 0x0F00 || keychar == 'T' || keychar == 'V' || keychar == 'L' || keychar == 'S')
  1085.          {
  1086.             int    PrevMode = EditMode;
  1087.             SelPtr NewSel;
  1088.  
  1089.             if ((key & 0x00FF) == 0x0009) /* 'Tab' */
  1090.             {
  1091.                switch (EditMode)
  1092.                {
  1093.                case OBJ_THINGS:
  1094.                   EditMode = OBJ_VERTEXES;
  1095.                   break;
  1096.                case OBJ_VERTEXES:
  1097.                   EditMode = OBJ_LINEDEFS;
  1098.                   break;
  1099.                case OBJ_LINEDEFS:
  1100.                   EditMode = OBJ_SECTORS;
  1101.                   break;
  1102.                case OBJ_SECTORS:
  1103.                   EditMode = OBJ_THINGS;
  1104.                   break;
  1105.                }
  1106.             }
  1107.             else if ((key & 0xFF00) == 0x0F00) /* 'Shift-Tab' */
  1108.             {
  1109.                switch (EditMode)
  1110.                {
  1111.                case OBJ_THINGS:
  1112.                   EditMode = OBJ_SECTORS;
  1113.                   break;
  1114.                case OBJ_VERTEXES:
  1115.                   EditMode = OBJ_THINGS;
  1116.                   break;
  1117.                case OBJ_LINEDEFS:
  1118.                   EditMode = OBJ_VERTEXES;
  1119.                   break;
  1120.                case OBJ_SECTORS:
  1121.                   EditMode = OBJ_LINEDEFS;
  1122.                   break;
  1123.                }
  1124.             }
  1125.             else
  1126.             {
  1127.                if (keychar == 'T')
  1128.                   EditMode = OBJ_THINGS;
  1129.                else if (keychar == 'V')
  1130.                   EditMode = OBJ_VERTEXES;
  1131.                else if (keychar == 'L')
  1132.                   EditMode = OBJ_LINEDEFS;
  1133.                else if (keychar == 'S')
  1134.                   EditMode = OBJ_SECTORS;
  1135.                /* unselect all */
  1136.                ForgetSelection( &Selected);
  1137.             }
  1138.  
  1139.             /* special cases for the selection list... */
  1140.             if (Selected)
  1141.             {
  1142.                /* select all LineDefs bound to the selected Sectors */
  1143.                if (PrevMode == OBJ_SECTORS && EditMode == OBJ_LINEDEFS)
  1144.                {
  1145.                   int l, sd;
  1146.  
  1147.                   ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1148.                   NewSel = NULL;
  1149.                   for (l = 0; l < NumLineDefs; l++)
  1150.                   {
  1151.                      sd = LineDefs[ l].sidedef1;
  1152.                      if (sd >= 0 && IsSelected( Selected, SideDefs[ sd].sector))
  1153.                         SelectObject( &NewSel, l);
  1154.                      else
  1155.                      {
  1156.                         sd = LineDefs[ l].sidedef2;
  1157.                         if (sd >= 0 && IsSelected( Selected, SideDefs[ sd].sector))
  1158.                            SelectObject( &NewSel, l);
  1159.                      }
  1160.                   }
  1161.                   ForgetSelection( &Selected);
  1162.                   Selected = NewSel;
  1163.                }
  1164.                /* select all Vertices bound to the selected LineDefs */
  1165.                else if (PrevMode == OBJ_LINEDEFS && EditMode == OBJ_VERTEXES)
  1166.                {
  1167.                   ObjectsNeeded( OBJ_LINEDEFS, 0);
  1168.                   NewSel = NULL;
  1169.                   while (Selected)
  1170.                   {
  1171.                      if (!IsSelected( NewSel, LineDefs[ Selected->objnum].start))
  1172.                         SelectObject( &NewSel, LineDefs[ Selected->objnum].start);
  1173.                      if (!IsSelected( NewSel, LineDefs[ Selected->objnum].end))
  1174.                         SelectObject( &NewSel, LineDefs[ Selected->objnum].end);
  1175.                      UnSelectObject( &Selected, Selected->objnum);
  1176.                   }
  1177.                   Selected = NewSel;
  1178.                }
  1179.                /* select all Sectors that have their LineDefs selected */
  1180.                else if (PrevMode == OBJ_LINEDEFS && EditMode == OBJ_SECTORS)
  1181.                {
  1182.                   int l, sd;
  1183.  
  1184.                   ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1185.                   NewSel = NULL;
  1186.                   /* select all Sectors... */
  1187.                   for (l = 0; l < NumSectors; l++)
  1188.                      SelectObject( &NewSel, l);
  1189.                   /* ... then unselect those that should not be in the list */
  1190.                   for (l = 0; l < NumLineDefs; l++)
  1191.                      if (!IsSelected( Selected, l))
  1192.                      {
  1193.                         sd = LineDefs[ l].sidedef1;
  1194.                         if (sd >= 0)
  1195.                            UnSelectObject( &NewSel, SideDefs[ sd].sector);
  1196.                         sd = LineDefs[ l].sidedef2;
  1197.                         if (sd >= 0)
  1198.                            UnSelectObject( &NewSel, SideDefs[ sd].sector);
  1199.                      }
  1200.                   ForgetSelection( &Selected);
  1201.                   Selected = NewSel;
  1202.                }
  1203.                /* select all LineDefs that have both ends selected */
  1204.                else if (PrevMode == OBJ_VERTEXES && EditMode == OBJ_LINEDEFS)
  1205.                {
  1206.                   int l;
  1207.  
  1208.                   ObjectsNeeded( OBJ_LINEDEFS, 0);
  1209.                   NewSel = NULL;
  1210.                   for (l = 0; l < NumLineDefs; l++)
  1211.                      if (IsSelected( Selected, LineDefs[ l].start) && IsSelected( Selected, LineDefs[ l].end))
  1212.                         SelectObject( &NewSel, l);
  1213.                   ForgetSelection( &Selected);
  1214.                   Selected = NewSel;
  1215.                }
  1216.                /* unselect all */
  1217.                else
  1218.                   ForgetSelection( &Selected);
  1219.             }
  1220.             if (GetMaxObjectNum( EditMode) >= 0 && Select0 == TRUE)
  1221.                CurObject = 0;
  1222.             else
  1223.                CurObject = -1;
  1224.             OldObject = -1;
  1225.             DragObject = FALSE;
  1226.             StretchSelBox = FALSE;
  1227.             RedrawMap = TRUE;
  1228.          }
  1229.  
  1230.          /* user wants to change the grid scale */
  1231.          else if (keychar == 'G')
  1232.          {
  1233.             if ((altkey & 0x03) == 0x00)  /* no shift keys */
  1234.             {
  1235.                if (GridScale == 0)
  1236.                   GridScale = 256;
  1237.                else if (GridScale > 8)
  1238.                   GridScale /= 2;
  1239.                else
  1240.                   GridScale = 0;
  1241.             }
  1242.             else
  1243.             {
  1244.                if (GridScale == 0)
  1245.                   GridScale = 8;
  1246.                else if (GridScale < 256)
  1247.                   GridScale *= 2;
  1248.                else
  1249.                   GridScale = 0;
  1250.             }
  1251.             RedrawMap = TRUE;
  1252.          }
  1253.  
  1254.          /* user wants to display or hide the grid */
  1255.          else if (keychar == 'H')
  1256.          {
  1257.             if ((altkey & 0x03) != 0x00)  /* shift key pressed */
  1258.                GridScale = 0;
  1259.             else
  1260.                GridShown = !GridShown;
  1261.             RedrawMap = TRUE;
  1262.          }
  1263.  
  1264.          /* user wants to toggle the rulers */
  1265.          else if (keychar == 'R')
  1266.             ShowRulers = !ShowRulers;
  1267.  
  1268.          /* user wants to toggle drag mode */
  1269.          else if (keychar == 'D')
  1270.          {
  1271.             StretchSelBox = FALSE;
  1272.             if (DragObject)
  1273.             {
  1274.                DragObject = FALSE;
  1275.                if (EditMode == OBJ_VERTEXES)
  1276.                {
  1277.                   if (Selected == NULL && CurObject >= 0)
  1278.                   {
  1279.                      SelectObject( &Selected, CurObject);
  1280.                      if (AutoMergeVertices( &Selected))
  1281.                         RedrawMap = TRUE;
  1282.                      ForgetSelection( &Selected);
  1283.                   }
  1284.                   else
  1285.                      if (AutoMergeVertices( &Selected))
  1286.                         RedrawMap = TRUE;
  1287.                }
  1288.                else if (EditMode == OBJ_LINEDEFS)
  1289.                {
  1290.                   SelPtr NewSel, cur;
  1291.  
  1292.                   ObjectsNeeded( OBJ_LINEDEFS, 0);
  1293.                   NewSel = NULL;
  1294.                   if (Selected == NULL && CurObject >= 0)
  1295.                   {
  1296.                      SelectObject( &NewSel, LineDefs[ CurObject].start);
  1297.                      SelectObject( &NewSel, LineDefs[ CurObject].end);
  1298.                   }
  1299.                   else
  1300.                   {
  1301.                      for (cur = Selected; cur; cur = cur->next)
  1302.                      {
  1303.                         if (!IsSelected( NewSel, LineDefs[ cur->objnum].start))
  1304.                            SelectObject( &NewSel, LineDefs[ cur->objnum].start);
  1305.                         if (!IsSelected( NewSel, LineDefs[ cur->objnum].end))
  1306.                            SelectObject( &NewSel, LineDefs[ cur->objnum].end);
  1307.                      }
  1308.                   }
  1309.                   if (AutoMergeVertices( &NewSel))
  1310.                      RedrawMap = TRUE;
  1311.                   ForgetSelection( &NewSel);
  1312.                }
  1313.             }
  1314.             else
  1315.             {
  1316.                DragObject = TRUE;
  1317.                if (EditMode == OBJ_THINGS && CurObject >= 0)
  1318.                   MoveObjectsToCoords( EditMode, NULL, Things[ CurObject].xpos, Things[ CurObject].ypos, 0);
  1319.                else if (EditMode == OBJ_VERTEXES && CurObject >= 0)
  1320.                   MoveObjectsToCoords( EditMode, NULL, Vertexes[ CurObject].x, Vertexes[ CurObject].y, 0);
  1321.                else
  1322.                   MoveObjectsToCoords( EditMode, NULL, MAPX( PointerX), MAPY( PointerY), GridScale);
  1323.             }
  1324.          }
  1325.  
  1326.          /* user wants to select the next or previous object */
  1327.          else if (keychar == 'N' || keychar == '>')
  1328.          {
  1329.             if (CurObject < GetMaxObjectNum( EditMode))
  1330.                CurObject++;
  1331.             else if (GetMaxObjectNum( EditMode) >= 0)
  1332.                CurObject = 0;
  1333.             else
  1334.                CurObject = -1;
  1335.             RedrawMap = TRUE;
  1336.          }
  1337.          else if (keychar == 'P' || keychar == '<')
  1338.          {
  1339.             if (CurObject > 0)
  1340.                CurObject--;
  1341.             else
  1342.                CurObject = GetMaxObjectNum( EditMode);
  1343.             RedrawMap = TRUE;
  1344.          }
  1345.          else if (keychar == 'J' || keychar == '#')
  1346.          {
  1347.             OldObject = InputObjectNumber( -1, -1, EditMode, CurObject);
  1348.             if (OldObject >= 0)
  1349.             {
  1350.                CurObject = OldObject;
  1351.                GoToObject( EditMode, CurObject);
  1352.             }
  1353.             else
  1354.                OldObject = CurObject;
  1355.             RedrawMap = TRUE;
  1356.          }
  1357.  
  1358.          /* user wants to mark/unmark an object or a group of objects */
  1359.          else if (keychar == 'M')
  1360.          {
  1361.             if (StretchSelBox)
  1362.             {
  1363.                SelPtr oldsel;
  1364.  
  1365.                /* select all objects in the selection box */
  1366.                StretchSelBox = FALSE;
  1367.                RedrawMap = TRUE;
  1368.                /* additive selection box or not? */
  1369.                if (AdditiveSelBox == FALSE)
  1370.                   ForgetSelection( &Selected);
  1371.                else
  1372.                   oldsel = Selected;
  1373.                Selected = SelectObjectsInBox( EditMode, SelBoxX, SelBoxY, MAPX( PointerX), MAPY( PointerY));
  1374.                if (AdditiveSelBox == TRUE)
  1375.                   while (oldsel != NULL)
  1376.                   {
  1377.                      if (! IsSelected( Selected, oldsel->objnum))
  1378.                         SelectObject( &Selected, oldsel->objnum);
  1379.                      UnSelectObject( &oldsel, oldsel->objnum);
  1380.                   }
  1381.                if (Selected)
  1382.                {
  1383.                   CurObject = Selected->objnum;
  1384.                   PlaySound( 440, 10);
  1385.                }
  1386.                else
  1387.                   CurObject = -1;
  1388.             }
  1389.             else if ((altkey & 0x03) == 0x00)  /* no shift keys */
  1390.             {
  1391.                if (CurObject >= 0)
  1392.                {
  1393.                   /* mark or unmark one object */
  1394.                   if (IsSelected( Selected, CurObject))
  1395.                      UnSelectObject( &Selected, CurObject);
  1396.                   else
  1397.                      SelectObject( &Selected, CurObject);
  1398.                   if (UseMouse)
  1399.                      HideMousePointer();
  1400.                   HighlightObject( EditMode, CurObject, GREEN);
  1401.                   if (UseMouse)
  1402.                      ShowMousePointer();
  1403.                   if (Selected)
  1404.                      PlaySound( 440, 10);
  1405.                   DragObject = FALSE;
  1406.                }
  1407.                else
  1408.                   Beep();
  1409.             }
  1410.             else
  1411.             {
  1412.                /* begin "stretch selection box" mode */
  1413.                SelBoxX = MAPX( PointerX);
  1414.                SelBoxY = MAPY( PointerY);
  1415.                StretchSelBox = TRUE;
  1416.                DragObject = FALSE;
  1417.             }
  1418.          }
  1419.  
  1420.          /* user wants to clear all marks and redraw the map */
  1421.          else if (keychar == 'C')
  1422.          {
  1423.             ForgetSelection( &Selected);
  1424.             RedrawMap = TRUE;
  1425.             DragObject = FALSE;
  1426.             StretchSelBox = FALSE;
  1427.          }
  1428.  
  1429.          /* user wants to copy a group of objects */
  1430.          else if (keychar == 'O' && CurObject >= 0)
  1431.          {
  1432.             /* copy the object(s) */
  1433.             if (Selected == NULL)
  1434.                SelectObject( &Selected, CurObject);
  1435.             CopyObjects( EditMode, Selected);
  1436.             /* enter drag mode */
  1437.             DragObject = TRUE;
  1438.             CurObject = Selected->objnum;
  1439.             if (EditMode == OBJ_THINGS)
  1440.                MoveObjectsToCoords( EditMode, NULL, Things[ CurObject].xpos, Things[ CurObject].ypos, 0);
  1441.             else if (EditMode == OBJ_VERTEXES)
  1442.                MoveObjectsToCoords( EditMode, NULL, Vertexes[ CurObject].x, Vertexes[ CurObject].y, 0);
  1443.             else
  1444.                MoveObjectsToCoords( EditMode, NULL, MAPX( PointerX), MAPY( PointerY), GridScale);
  1445.             RedrawMap = TRUE;
  1446.             StretchSelBox = FALSE;
  1447.          }
  1448.  
  1449.          /* user wants to edit the current object */
  1450.          else if ((key & 0x00FF) == 0x000D && CurObject >= 0) /* 'Enter' */
  1451.          {
  1452.             if (Selected)
  1453.                EditObjectsInfo( 0, 30, EditMode, Selected);
  1454.             else
  1455.             {
  1456.                SelectObject( &Selected, CurObject);
  1457.                EditObjectsInfo( 0, 30, EditMode, Selected);
  1458.                UnSelectObject( &Selected, CurObject);
  1459.             }
  1460.             RedrawMap = TRUE;
  1461.             DragObject = FALSE;
  1462.             StretchSelBox = FALSE;
  1463.          }
  1464.  
  1465.          /* user wants to delete the current object */
  1466.          else if ((key & 0xFF00) == 0x5300 && CurObject >= 0) /* 'Del' */
  1467.          {
  1468.             if (EditMode == OBJ_THINGS || Expert || Confirm( -1, -1,
  1469.                                 (Selected ? "Do you really want to delete these objects?" : "Do you really want to delete this object?"),
  1470.                                 (Selected ? "This will also delete the objects bound to them." : "This will also delete the objects bound to it.")))
  1471.             {
  1472.                if (Selected)
  1473.                   DeleteObjects( EditMode, &Selected);
  1474.                else
  1475.                   DeleteObject( EditMode, CurObject);
  1476.                CurObject = -1;
  1477.             }
  1478.             DragObject = FALSE;
  1479.             StretchSelBox = FALSE;
  1480.             RedrawMap = TRUE;
  1481.          }
  1482.  
  1483.          /* user wants to insert a new object */
  1484.          else if ((key & 0xFF00) == 0x5200) /* 'Ins' */
  1485.          {
  1486.             SelPtr cur;
  1487.  
  1488.             /* first special case: if several Vertices are selected, add new LineDefs */
  1489.             if (EditMode == OBJ_VERTEXES && Selected != NULL && Selected->next != NULL)
  1490.             {
  1491.                int firstv;
  1492.  
  1493.                ObjectsNeeded( OBJ_LINEDEFS, 0);
  1494.                if (Selected->next->next != NULL)
  1495.                   firstv = Selected->objnum;
  1496.                else
  1497.                   firstv = -1;
  1498.                EditMode = OBJ_LINEDEFS;
  1499.                /* create LineDefs between the Vertices */
  1500.                for (cur = Selected; cur->next; cur = cur->next)
  1501.                {
  1502.                   /* check if there is already a LineDef between the two Vertices */
  1503.                   for (CurObject = 0; CurObject < NumLineDefs; CurObject++)
  1504.                      if ((LineDefs[ CurObject].start == cur->next->objnum && LineDefs[ CurObject].end == cur->objnum)
  1505.                       || (LineDefs[ CurObject].end == cur->next->objnum && LineDefs[ CurObject].start == cur->objnum))
  1506.                         break;
  1507.                   if (CurObject < NumLineDefs)
  1508.                      cur->objnum = CurObject;
  1509.                   else
  1510.                   {
  1511.                      InsertObject( OBJ_LINEDEFS, -1, 0, 0);
  1512.                      CurObject = NumLineDefs - 1;
  1513.                      LineDefs[ CurObject].start = cur->next->objnum;
  1514.                      LineDefs[ CurObject].end = cur->objnum;
  1515.                      cur->objnum = CurObject;
  1516.                   }
  1517.                }
  1518.                /* close the polygon if there are more than 2 Vertices */
  1519.                if (firstv >= 0 && (altkey & 0x03) != 0x00)  /* shift key pressed */
  1520.                {
  1521.                   for (CurObject = 0; CurObject < NumLineDefs; CurObject++)
  1522.                      if ((LineDefs[ CurObject].start == firstv && LineDefs[ CurObject].end == cur->objnum)
  1523.                       || (LineDefs[ CurObject].end == firstv && LineDefs[ CurObject].start == cur->objnum))
  1524.                         break;
  1525.                   if (CurObject < NumLineDefs)
  1526.                      cur->objnum = CurObject;
  1527.                   else
  1528.                   {
  1529.                      InsertObject( OBJ_LINEDEFS, -1, 0, 0);
  1530.                      CurObject = NumLineDefs - 1;
  1531.                      LineDefs[ CurObject].start = firstv;
  1532.                      LineDefs[ CurObject].end = cur->objnum;
  1533.                      cur->objnum = CurObject;
  1534.                   }
  1535.                }
  1536.                else
  1537.                   UnSelectObject( &Selected, cur->objnum);
  1538.             }
  1539.             /* second special case: if several LineDefs are selected, add new SideDefs and one Sector */
  1540.             else if (EditMode == OBJ_LINEDEFS && Selected != NULL)
  1541.             {
  1542.                ObjectsNeeded( OBJ_LINEDEFS, 0);
  1543.                for (cur = Selected; cur; cur = cur->next)
  1544.                   if (LineDefs[ cur->objnum].sidedef1 >= 0 && LineDefs[ cur->objnum].sidedef2 >= 0)
  1545.                   {
  1546.                      char msg[ 80];
  1547.  
  1548.                      Beep();
  1549.                      sprintf( msg, "LineDef #%d already has two SideDefs", cur->objnum);
  1550.                      Notify( -1, -1, "Error: cannot add the new Sector", msg);
  1551.                      break;
  1552.                   }
  1553.                if (cur == NULL)
  1554.                {
  1555.                   EditMode = OBJ_SECTORS;
  1556.                   InsertObject( OBJ_SECTORS, -1, 0, 0);
  1557.                   CurObject = NumSectors - 1;
  1558.                   for (cur = Selected; cur; cur = cur->next)
  1559.                   {
  1560.                      InsertObject( OBJ_SIDEDEFS, -1, 0, 0);
  1561.                      SideDefs[ NumSideDefs - 1].sector = CurObject;
  1562.                      ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1563.                      if (LineDefs[ cur->objnum].sidedef1 >= 0)
  1564.                      {
  1565.                         int s;
  1566.  
  1567.                         s = SideDefs[ LineDefs[ cur->objnum].sidedef1].sector;
  1568.                         if (s >= 0)
  1569.                         {
  1570.                            Sectors[ CurObject].floorh = Sectors[ s].floorh;
  1571.                            Sectors[ CurObject].ceilh = Sectors[ s].ceilh;
  1572.                            strncpy( Sectors[ CurObject].floort, Sectors[ s].floort, 8);
  1573.                            strncpy( Sectors[ CurObject].ceilt, Sectors[ s].ceilt, 8);
  1574.                            Sectors[ CurObject].light = Sectors[ s].light;
  1575.                         }
  1576.                         LineDefs[ cur->objnum].sidedef2 = NumSideDefs - 1;
  1577.                         LineDefs[ cur->objnum].flags = 4;
  1578.                         strncpy( SideDefs[ NumSideDefs - 1].tex3, "-", 8);
  1579.                         strncpy( SideDefs[ LineDefs[ cur->objnum].sidedef1].tex3, "-", 8);
  1580.                      }
  1581.                      else
  1582.                         LineDefs[ cur->objnum].sidedef1 = NumSideDefs - 1;
  1583.                   }
  1584.                   ForgetSelection( &Selected);
  1585.                   SelectObject( &Selected, CurObject);
  1586.                }
  1587.             }
  1588.             /* normal case: add a new object of the current type */
  1589.             else
  1590.             {
  1591.                ForgetSelection( &Selected);
  1592.                if (GridScale > 0)
  1593.                   InsertObject( EditMode, CurObject, (MAPX( PointerX) + GridScale / 2) & ~(GridScale - 1), (MAPY( PointerY) + GridScale / 2) & ~(GridScale - 1));
  1594.                else
  1595.                   InsertObject( EditMode, CurObject, MAPX( PointerX), MAPY( PointerY));
  1596.                CurObject = GetMaxObjectNum( EditMode);
  1597.                if (EditMode == OBJ_LINEDEFS)
  1598.                {
  1599.                   if (! Input2VertexNumbers( -1, -1, "Choose the two vertices for the new LineDef",
  1600.                                              &(LineDefs[ CurObject].start), &(LineDefs[ CurObject].end)))
  1601.                   {
  1602.                      DeleteObject( EditMode, CurObject);
  1603.                      CurObject = -1;
  1604.                   }
  1605.                }
  1606.                else if (EditMode == OBJ_VERTEXES)
  1607.                {
  1608.                   SelectObject( &Selected, CurObject);
  1609.                   if (AutoMergeVertices( &Selected))
  1610.                      RedrawMap = TRUE;
  1611.                   ForgetSelection( &Selected);
  1612.                }
  1613.             }
  1614.             DragObject = FALSE;
  1615.             StretchSelBox = FALSE;
  1616.             RedrawMap = TRUE;
  1617.          }
  1618.  
  1619.          /* user likes music */
  1620.          else if (key)
  1621.             Beep();
  1622.  
  1623.          /* redraw the (keyboard) pointer */
  1624.          if (FakeCursor || ShowRulers)
  1625.          {
  1626.             HideMousePointer();
  1627.             DrawPointer( ShowRulers);
  1628.             ShowMousePointer();
  1629.          }
  1630.       }
  1631.  
  1632.       /* check if Scroll Lock is off */
  1633.       if ((bioskey( 2) & 0x10) == 0x00)
  1634.       {
  1635.          /* move the map if the pointer is near the edge of the screen */
  1636.          if (PointerY <= (UseMouse ? 2 : 20)) // Changed this
  1637.          {
  1638.             if (! UseMouse)
  1639.                PointerY += MoveSpeed;
  1640.             if (MAPY( ScrCenterY) < MapMaxY)
  1641.             {
  1642.                OrigY += (int) (MoveSpeed * 2.0 / Scale);
  1643.                RedrawMap = TRUE;
  1644.             }
  1645.          }
  1646.          if (PointerY >= ScrMaxY - (UseMouse ? 8 : 20)) // Changed this
  1647.          {
  1648.             if (! UseMouse)
  1649.                PointerY -= MoveSpeed;
  1650.             if (MAPY( ScrCenterY) > MapMinY)
  1651.             {
  1652.                OrigY -= (int) (MoveSpeed * 2.0 / Scale);
  1653.                RedrawMap = TRUE;
  1654.             }
  1655.          }
  1656.          if (PointerX <= (UseMouse ? 8 : 20)) // Changed this
  1657.          {
  1658.             if (! UseMouse)
  1659.                PointerX += MoveSpeed;
  1660.             if (MAPX( ScrCenterX) > MapMinX)
  1661.             {
  1662.                OrigX -= (int) (MoveSpeed * 2.0 / Scale);
  1663.                RedrawMap = TRUE;
  1664.             }
  1665.          }
  1666.          if (PointerX >= ScrMaxX - (UseMouse ? 8 : 20)) // Changed this
  1667.          {
  1668.             if (! UseMouse)
  1669.                PointerX -= MoveSpeed;
  1670.             if (MAPX( ScrCenterX) < MapMaxX)
  1671.             {
  1672.                OrigX += (int) (MoveSpeed * 2.0 / Scale);
  1673.                RedrawMap = TRUE;
  1674.             }
  1675.          }
  1676.       }
  1677.    }
  1678. }
  1679.  
  1680.  
  1681.  
  1682. /*
  1683.    draw the actual game map
  1684. */
  1685.  
  1686. void DrawMap( int editmode, int grid, Bool drawgrid) /* SWAP! */
  1687. {
  1688.    int  n, m;
  1689.  
  1690.    /* clear the screen */
  1691.    ClearScreen();
  1692.  
  1693.    /* draw the grid */
  1694.    if (drawgrid == TRUE && grid > 0)
  1695.    {
  1696.       int mapx0 = MAPX( 0) & ~(grid - 1);
  1697.       int mapx1 = (MAPX( ScrMaxX) + grid) & ~(grid - 1);
  1698.       int mapy0 = (MAPY( ScrMaxY) - grid) & ~(grid - 1);
  1699.       int mapy1 = MAPY( 0) & ~(grid - 1);
  1700.  
  1701.       SetColor( BLUE);
  1702.       for (n = mapx0; n <= mapx1; n += grid)
  1703.          DrawMapLine( n, mapy0, n, mapy1);
  1704.       for (n = mapy0; n <= mapy1; n += grid)
  1705.          DrawMapLine( mapx0, n, mapx1, n);
  1706.    }
  1707.  
  1708.    /* draw the linedefs to form the map */
  1709.    switch (editmode)
  1710.    {
  1711.    case OBJ_THINGS:
  1712.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  1713.       for (n = 0; n < NumLineDefs; n++)
  1714.       {
  1715.          if (LineDefs[ n].flags & 1)
  1716.             SetColor( WHITE);
  1717.          else
  1718.             SetColor( LIGHTGRAY);
  1719.          DrawMapLine( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  1720.                       Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  1721.       }
  1722.       break;
  1723.    case OBJ_VERTEXES:
  1724.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  1725.       SetColor( LIGHTGRAY);
  1726.       for (n = 0; n < NumLineDefs; n++)
  1727.          DrawMapVector( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  1728.                         Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  1729.       break;
  1730.    case OBJ_LINEDEFS:
  1731.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  1732.       for (n = 0; n < NumLineDefs; n++)
  1733.       {
  1734.          if (LineDefs[ n].type > 0)
  1735.          {
  1736.             if (LineDefs[ n].tag > 0)
  1737.                SetColor( LIGHTMAGENTA);
  1738.             else
  1739.                SetColor( LIGHTGREEN);
  1740.          }
  1741.          else if (LineDefs[ n].tag > 0)
  1742.             SetColor( LIGHTRED);
  1743.          else if (LineDefs[ n].flags & 1)
  1744.             SetColor( WHITE);
  1745.          else
  1746.             SetColor( LIGHTGRAY);
  1747.          DrawMapLine( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  1748.                       Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  1749.       }
  1750.       break;
  1751.    case OBJ_SECTORS:
  1752.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_SIDEDEFS, 0);
  1753.       for (n = 0; n < NumLineDefs; n++)
  1754.       {
  1755.          if ((m = LineDefs[ n].sidedef1) < 0 || (m = SideDefs[ m].sector) < 0)
  1756.             SetColor( LIGHTRED);
  1757.          else
  1758.          {
  1759.             if (Sectors[ m].tag > 0)
  1760.                SetColor( LIGHTGREEN);
  1761.             else if (Sectors[ m].special > 0)
  1762.                SetColor( LIGHTCYAN);
  1763.             else if (LineDefs[ n].flags & 1)
  1764.                SetColor( WHITE);
  1765.             else
  1766.                SetColor( LIGHTGRAY);
  1767.             if ((m = LineDefs[ n].sidedef2) >= 0)
  1768.             {
  1769.                if ((m = SideDefs[ m].sector) < 0)
  1770.                   SetColor( LIGHTRED);
  1771.                else if (Sectors[ m].tag > 0)
  1772.                   SetColor( LIGHTGREEN);
  1773.                else if (Sectors[ m].special > 0)
  1774.                   SetColor( LIGHTCYAN);
  1775.             }
  1776.          }
  1777.          ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  1778.          DrawMapLine( Vertexes[ LineDefs[ n].start].x, Vertexes[ LineDefs[ n].start].y,
  1779.                       Vertexes[ LineDefs[ n].end].x, Vertexes[ LineDefs[ n].end].y);
  1780.       }
  1781.       break;
  1782.    }
  1783.  
  1784.    /* draw in the vertices */
  1785.    if (editmode == OBJ_VERTEXES)
  1786.    {
  1787.       SetColor( LIGHTGREEN);
  1788.       for (n = 0; n < NumVertexes; n++)
  1789.       {
  1790.          DrawMapLine( Vertexes[ n].x - OBJSIZE, Vertexes[ n].y - OBJSIZE, Vertexes[ n].x + OBJSIZE, Vertexes[ n].y + OBJSIZE);
  1791.          DrawMapLine( Vertexes[ n].x + OBJSIZE, Vertexes[ n].y - OBJSIZE, Vertexes[ n].x - OBJSIZE, Vertexes[ n].y + OBJSIZE);
  1792.       }
  1793.    }
  1794.  
  1795.    /* draw in the things */
  1796.    ObjectsNeeded( OBJ_THINGS, 0);
  1797.    if (editmode == OBJ_THINGS)
  1798.    {
  1799.       for (n = 0; n < NumThings; n++)
  1800.       {
  1801.          m = GetThingRadius( Things[ n].type);
  1802.          SetColor( GetThingColour( Things[ n].type));
  1803.          DrawMapLine( Things[ n].xpos - m, Things[ n].ypos, Things[ n].xpos + m, Things[ n].ypos);
  1804.          DrawMapLine( Things[ n].xpos, Things[ n].ypos - m, Things[ n].xpos, Things[ n].ypos + m);
  1805.          DrawMapCircle( Things[ n].xpos, Things[ n].ypos, m);
  1806.       }
  1807.    }
  1808.    else
  1809.    {
  1810.       SetColor( LIGHTGRAY);
  1811.       for (n = 0; n < NumThings; n++)
  1812.       {
  1813.          DrawMapLine( Things[ n].xpos - OBJSIZE, Things[ n].ypos, Things[ n].xpos + OBJSIZE, Things[ n].ypos);
  1814.          DrawMapLine( Things[ n].xpos, Things[ n].ypos - OBJSIZE, Things[ n].xpos, Things[ n].ypos + OBJSIZE);
  1815.       }
  1816.    }
  1817.  
  1818.    /* draw in the title bar */
  1819.    DrawScreenBox3D( 0, 0, ScrMaxX, 16);
  1820.    SetColor( WHITE);
  1821.    DrawScreenText( 20,  4, "File  Edit  Search  Modes  Misc  Objects  Check");
  1822.    DrawScreenText( 20,  6, "_     _     _       _       _    _        _    ");
  1823.    DrawScreenText( ScrMaxX - 45, 4, "Help");
  1824.    DrawScreenText( ScrMaxX - 45, 6, "_   ");
  1825.  
  1826.    /* draw the bottom line, if needed */
  1827.    if (InfoShown)
  1828.    {
  1829.       DrawScreenBox3D( 0, ScrMaxY - 11, ScrMaxX, ScrMaxY);
  1830.       if (MadeMapChanges == TRUE)
  1831.          DrawScreenText( 5, ScrMaxY - 8, "Editing %s on %s #", GetEditModeName( editmode), Level->dir.name);
  1832.       else if (MadeChanges == TRUE)
  1833.          DrawScreenText( 5, ScrMaxY - 8, "Editing %s on %s *", GetEditModeName( editmode), Level->dir.name);
  1834.       else
  1835.          DrawScreenText( 5, ScrMaxY - 8, "Editing %s on %s", GetEditModeName( editmode), Level->dir.name);
  1836.       if (Scale < 1.0)
  1837.          DrawScreenText( ScrMaxX - 176, ScrMaxY - 8, "Scale: 1/%d  Grid: %d", (int) (1.0 / Scale + 0.5), grid);
  1838.       else
  1839.          DrawScreenText( ScrMaxX - 176, ScrMaxY - 8, "Scale: %d/1  Grid: %d", (int) Scale, grid);
  1840.       if (farcoreleft() < 50000L)
  1841.       {
  1842.          if (farcoreleft() < 20000L)
  1843.             SetColor( LIGHTRED);
  1844.          else
  1845.             SetColor( RED);
  1846.       }
  1847.       DrawScreenText( ScrCenterX - ((editmode == OBJ_LINEDEFS) ? 10 : 50), ScrMaxY - 8, "Free mem: %lu", farcoreleft());
  1848.    }
  1849. }
  1850.  
  1851.  
  1852.  
  1853. /*
  1854.    center the map around the given coords
  1855. */
  1856.  
  1857. void CenterMapAroundCoords( int xpos, int ypos)
  1858. {
  1859.    OrigX = xpos;
  1860.    OrigY = ypos;
  1861.    PointerX = ScrCenterX;
  1862.    PointerY = ScrCenterY;
  1863. }
  1864.  
  1865.  
  1866.  
  1867. /*
  1868.    center the map around the object and zoom in if necessary
  1869. */
  1870.  
  1871. void GoToObject( int objtype, int objnum) /* SWAP! */
  1872. {
  1873.    int   xpos, ypos;
  1874.    int   xpos2, ypos2;
  1875.    int   n;
  1876.    int   sd1, sd2;
  1877.    float oldscale;
  1878.  
  1879.    GetObjectCoords( objtype, objnum, &xpos, &ypos);
  1880.    CenterMapAroundCoords( xpos, ypos);
  1881.    oldscale = Scale;
  1882.  
  1883.    /* zoom in until the object can be selected */
  1884.    while (Scale < 4.0 && GetCurObject( objtype, MAPX( PointerX - 4), MAPY( PointerY - 4), MAPX( PointerX + 4), MAPY( PointerY + 4)) != objnum)
  1885.    {
  1886.       if (Scale < 1.0)
  1887.          Scale = 1.0 / ((1.0 / Scale) - 1.0);
  1888.       else
  1889.          Scale = Scale * 2.0;
  1890.    }
  1891.  
  1892.    /* Special case for Sectors: if several Sectors are one inside another, then    */
  1893.    /* zooming in on the center won't help.  So I choose a LineDef that borders the */
  1894.    /* Sector, move a few pixels towards the inside of the Sector, then zoom in.    */
  1895.    if (objtype == OBJ_SECTORS && GetCurObject( OBJ_SECTORS, OrigX, OrigY, OrigX, OrigY) != objnum)
  1896.    {
  1897.       /* restore the Scale */
  1898.       Scale = oldscale;
  1899.       for (n = 0; n < NumLineDefs; n++)
  1900.       {
  1901.          ObjectsNeeded( OBJ_LINEDEFS, 0);
  1902.          sd1 = LineDefs[ n].sidedef1;
  1903.          sd2 = LineDefs[ n].sidedef2;
  1904.          ObjectsNeeded( OBJ_SIDEDEFS, 0);
  1905.          if (sd1 >= 0 && SideDefs[ sd1].sector == objnum)
  1906.             break;
  1907.          if (sd2 >= 0 && SideDefs[ sd2].sector == objnum)
  1908.             break;
  1909.       }
  1910.       if (n < NumLineDefs)
  1911.       {
  1912.          GetObjectCoords( OBJ_LINEDEFS, n, &xpos2, &ypos2);
  1913.          n = ComputeDist( abs( xpos - xpos2), abs( ypos - ypos2)) / 7;
  1914.          if (n <= 1)
  1915.            n = 2;
  1916.          xpos = xpos2 + (xpos - xpos2) / n;
  1917.          ypos = ypos2 + (ypos - ypos2) / n;
  1918.          CenterMapAroundCoords( xpos, ypos);
  1919.          /* zoom in until the sector can be selected */
  1920.          while (Scale > 4.0 && GetCurObject( OBJ_SECTORS, OrigX, OrigY, OrigX, OrigY) != objnum)
  1921.          {
  1922.             if (Scale < 1.0)
  1923.                Scale = 1.0 / ((1.0 / Scale) - 1.0);
  1924.             else
  1925.                Scale = Scale / 2.0;
  1926.          }
  1927.       }
  1928.    }
  1929.    if (UseMouse)
  1930.       SetMouseCoords( PointerX, PointerY);
  1931. }
  1932.  
  1933.  
  1934.  
  1935. /* end of file */
  1936.