rnd-20070221-1-src
[rocksndiamonds.git] / src / editor.c
index 1ec48e18f5c19486e62cd27a36a7ced35de26ab0..bf5640441d666ebba79d3877e9b5c47654808780 100644 (file)
@@ -3119,7 +3119,11 @@ static int ed_fieldx = MAX_ED_FIELDX - 1, ed_fieldy = MAX_ED_FIELDY - 1;
 /* actual position of level editor drawing area in level playfield */
 static int level_xpos = -1, level_ypos = -1;
 
+#if 1
+#define IN_ED_FIELD(x,y)       IN_FIELD(x, y, ed_fieldx, ed_fieldy)
+#else
 #define IN_ED_FIELD(x,y)  ((x)>=0 && (x)<ed_fieldx && (y)>=0 &&(y)<ed_fieldy)
+#endif
 
 /* drawing elements on the three mouse buttons */
 static int new_element1 = EL_WALL;
@@ -3170,6 +3174,7 @@ static int properties_element = 0;
 
 static short FieldBackup[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
 static short UndoBuffer[NUM_UNDO_STEPS][MAX_LEV_FIELDX][MAX_LEV_FIELDY];
+static short IntelliDrawBuffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
 static int undo_buffer_position = 0;
 static int undo_buffer_steps = 0;
 
@@ -8345,71 +8350,604 @@ static void UpdateCustomElementGraphicGadgets()
   }
 }
 
-static void DrawLineElement(int sx, int sy, int element, boolean change_level)
+static int getOpenDirectionFromTube(int element)
 {
-  int lx = sx + level_xpos;
-  int ly = sy + level_ypos;
+  switch (element)
+  {
+    case EL_TUBE_LEFT_UP:              return (MV_LEFT | MV_UP);
+    case EL_TUBE_LEFT_DOWN:            return (MV_LEFT | MV_DOWN);
+    case EL_TUBE_RIGHT_UP:             return (MV_RIGHT | MV_UP);
+    case EL_TUBE_RIGHT_DOWN:           return (MV_RIGHT | MV_DOWN);
+    case EL_TUBE_HORIZONTAL:           return (MV_HORIZONTAL);
+    case EL_TUBE_HORIZONTAL_UP:                return (MV_HORIZONTAL | MV_UP);
+    case EL_TUBE_HORIZONTAL_DOWN:      return (MV_HORIZONTAL | MV_DOWN);
+    case EL_TUBE_VERTICAL:             return (MV_VERTICAL);
+    case EL_TUBE_VERTICAL_LEFT:                return (MV_VERTICAL | MV_LEFT);
+    case EL_TUBE_VERTICAL_RIGHT:       return (MV_VERTICAL | MV_RIGHT);
+    case EL_TUBE_ANY:                  return (MV_ANY_DIRECTION);
+  }
+
+  return MV_NONE;
+}
+
+static int getTubeFromOpenDirection(int direction)
+{
+  switch (direction)
+  {
+    case (MV_LEFT | MV_UP):            return EL_TUBE_LEFT_UP;
+    case (MV_LEFT | MV_DOWN):          return EL_TUBE_LEFT_DOWN;
+    case (MV_RIGHT | MV_UP):           return EL_TUBE_RIGHT_UP;
+    case (MV_RIGHT | MV_DOWN):         return EL_TUBE_RIGHT_DOWN;
+    case (MV_HORIZONTAL):              return EL_TUBE_HORIZONTAL;
+    case (MV_HORIZONTAL | MV_UP):      return EL_TUBE_HORIZONTAL_UP;
+    case (MV_HORIZONTAL | MV_DOWN):    return EL_TUBE_HORIZONTAL_DOWN;
+    case (MV_VERTICAL):                        return EL_TUBE_VERTICAL;
+    case (MV_VERTICAL | MV_LEFT):      return EL_TUBE_VERTICAL_LEFT;
+    case (MV_VERTICAL | MV_RIGHT):     return EL_TUBE_VERTICAL_RIGHT;
+    case (MV_ANY_DIRECTION):           return EL_TUBE_ANY;
+
+    /* if only one direction, fall back to simple tube with that direction */
+    case (MV_LEFT):                    return EL_TUBE_HORIZONTAL;
+    case (MV_RIGHT):                   return EL_TUBE_HORIZONTAL;
+    case (MV_UP):                      return EL_TUBE_VERTICAL;
+    case (MV_DOWN):                    return EL_TUBE_VERTICAL;
+  }
+
+  return EL_EMPTY;
+}
+
+static int getTubeFromOpenDirectionNotEmpty(int direction, int element_old)
+{
+  int element_new = getTubeFromOpenDirection(direction);
+
+  return (element_new != EL_EMPTY ? element_new : element_old);
+}
+
+static int getOpenDirectionFromBelt(int element)
+{
+  int belt_dir = getBeltDirFromBeltElement(element);
+
+  return (belt_dir == MV_LEFT ? MV_RIGHT :
+         belt_dir == MV_RIGHT ? MV_LEFT :
+         belt_dir == MV_NONE ? MV_HORIZONTAL : belt_dir);
+}
+
+static int getBeltFromNrAndOpenDirection(int nr, int direction)
+{
+  int belt_dir = (direction == MV_LEFT ? MV_RIGHT :
+                 direction == MV_RIGHT ? MV_LEFT :
+                 direction == MV_HORIZONTAL ? MV_NONE : direction);
+
+  return getBeltElementFromBeltNrAndBeltDir(nr, belt_dir);
+}
+
+static int getOpenDirectionFromPool(int element)
+{
+  switch (element)
+  {
+    case EL_ACID_POOL_TOPLEFT:         return (MV_DOWN | MV_RIGHT);
+    case EL_ACID_POOL_TOPRIGHT:                return (MV_DOWN | MV_LEFT);
+    case EL_ACID_POOL_BOTTOMLEFT:      return (MV_UP | MV_RIGHT);
+    case EL_ACID_POOL_BOTTOMRIGHT:     return (MV_UP | MV_LEFT);
+    case EL_ACID_POOL_BOTTOM:          return (MV_HORIZONTAL | MV_UP);
+    case EL_ACID:                      return (MV_HORIZONTAL | MV_DOWN);
+  }
+
+  return MV_NONE;
+}
+
+static int getPoolFromOpenDirection(int direction)
+{
+  switch (direction)
+  {
+    case (MV_DOWN | MV_RIGHT):         return EL_ACID_POOL_TOPLEFT;
+    case (MV_DOWN | MV_LEFT):          return EL_ACID_POOL_TOPRIGHT;
+    case (MV_UP | MV_RIGHT):           return EL_ACID_POOL_BOTTOMLEFT;
+    case (MV_UP | MV_LEFT):            return EL_ACID_POOL_BOTTOMRIGHT;
+    case (MV_HORIZONTAL | MV_UP):      return EL_ACID_POOL_BOTTOM;
+    case (MV_HORIZONTAL | MV_DOWN):    return EL_ACID;
+  }
+
+  return EL_EMPTY;
+}
+
+static int getPoolFromOpenDirection2(int direction, int help_element)
+{
+  int element = getPoolFromOpenDirection(direction);
+  int help_direction = getOpenDirectionFromPool(help_element);
+
+  if (element == EL_EMPTY)
+  {
+    int help_direction_vertical = help_direction & MV_VERTICAL;
+
+    element = getPoolFromOpenDirection(direction | help_direction_vertical);
+  }
+
+  if (element == EL_EMPTY)
+  {
+    int help_direction_horizontal = help_direction & MV_HORIZONTAL;
+
+    element = getPoolFromOpenDirection(direction | help_direction_horizontal);
+  }
+
+  return element;
+}
+
+static int getPoolFromOpenDirectionNotEmpty(int direction, int element_old)
+{
+  int element_new = getPoolFromOpenDirection2(direction, element_old);
+
+  return (element_new != EL_EMPTY ? element_new : element_old);
+}
+
+static int getClosedTube(int x, int y)
+{
+  static int xy[4][2] =
+  {
+    { -1, 0 },
+    { +1, 0 },
+    { 0, -1 },
+    { 0, +1 }
+  };
+  int element_old = IntelliDrawBuffer[x][y];
+  int tube_direction_old = getOpenDirectionFromTube(element_old);
+  int tube_direction_new = MV_NONE;
+  int i;
+
+  for (i = 0; i < NUM_DIRECTIONS; i++)
+  {
+    int xx = x + xy[i][0];
+    int yy = y + xy[i][1];
+    int dir = MV_DIR_FROM_BIT(i);
+    int dir_opposite = MV_DIR_OPPOSITE(dir);
+
+    if (IN_LEV_FIELD(xx, yy) && IS_TUBE(IntelliDrawBuffer[xx][yy]) &&
+       (tube_direction_old & dir) &&
+       (getOpenDirectionFromTube(IntelliDrawBuffer[xx][yy]) & dir_opposite))
+      tube_direction_new |= dir;
+  }
+
+  return getTubeFromOpenDirectionNotEmpty(tube_direction_new, element_old);
+}
+
+static int getClosedBelt(int x, int y)
+{
+  static int xy[2][2] =
+  {
+    { -1, 0 },
+    { +1, 0 },
+  };
+  int element_old = IntelliDrawBuffer[x][y];
+  int belt_nr = getBeltNrFromBeltElement(element_old);
+  int belt_direction_old = getOpenDirectionFromBelt(element_old);
+  int belt_direction_new = MV_NONE;
+  int i;
+
+  for (i = 0; i < 2; i++)
+  {
+    int xx = x + xy[i][0];
+    int yy = y + xy[i][1];
+    int dir = MV_DIR_FROM_BIT(i);
+    int dir_opposite = MV_DIR_OPPOSITE(dir);
+
+    if (IN_LEV_FIELD(xx, yy) && IS_BELT(IntelliDrawBuffer[xx][yy]) &&
+       (belt_direction_old & dir) &&
+       (getOpenDirectionFromBelt(IntelliDrawBuffer[xx][yy]) & dir_opposite))
+      belt_direction_new |= dir;
+  }
+
+  return getBeltFromNrAndOpenDirection(belt_nr, belt_direction_new);
+}
+
+static int getClosedPool(int x, int y)
+{
+  static int xy[4][2] =
+  {
+    { -1, 0 },
+    { +1, 0 },
+    { 0, -1 },
+    { 0, +1 }
+  };
+  int element_old = IntelliDrawBuffer[x][y];
+  int pool_direction_old = getOpenDirectionFromPool(element_old);
+  int pool_direction_new = MV_NONE;
+  int i;
+
+  for (i = 0; i < NUM_DIRECTIONS; i++)
+  {
+    int xx = x + xy[i][0];
+    int yy = y + xy[i][1];
+    int dir = MV_DIR_FROM_BIT(i);
+    int dir_opposite = MV_DIR_OPPOSITE(dir);
+
+    if (IN_LEV_FIELD(xx, yy) &&
+       IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[xx][yy]) &&
+       (pool_direction_old & dir) &&
+       (getOpenDirectionFromPool(IntelliDrawBuffer[xx][yy]) & dir_opposite))
+      pool_direction_new |= dir;
+  }
+
+  return getPoolFromOpenDirectionNotEmpty(pool_direction_new, element_old);
+}
+
+static void SetElementSimple(int x, int y, int element, boolean change_level)
+{
+  int sx = x - level_xpos;
+  int sy = y - level_ypos;
 
-  DrawMiniElement(sx, sy, (element < 0 ? Feld[lx][ly] : element));
+  IntelliDrawBuffer[x][y] = element;
 
   if (change_level)
-    Feld[lx][ly] = element;
+    Feld[x][y] = element;
+
+  if (IN_ED_FIELD(sx, sy))
+    DrawMiniElement(sx, sy, element);
 }
 
-static void DrawLine(int from_x, int from_y, int to_x, int to_y,
-                    int element, boolean change_level)
+static void SetElementIntelliDraw(int x, int y, int new_element,
+                                 boolean change_level)
 {
-  if (from_y == to_y)                  /* horizontal line */
+  static int last_x = -1;
+  static int last_y = -1;
+  int old_element = IntelliDrawBuffer[x][y];
+
+  if (new_element == EL_UNDEFINED)
   {
-    int x;
-    int y = from_y;
+    last_x = -1;
+    last_y = -1;
 
-    if (from_x > to_x)
-      swap_numbers(&from_x, &to_x);
+    return;
+  }
+
+  if (IS_TUBE(new_element))
+  {
+    static int xy[4][2] =
+    {
+      { -1, 0 },
+      { +1, 0 },
+      { 0, -1 },
+      { 0, +1 }
+    };
+    int last_element_new = EL_UNDEFINED;
+    int direction = MV_NONE;
+    int i;
+
+    /* if existing element is tube, keep all existing tube directions */
+    if (IS_TUBE(old_element))
+      direction |= getOpenDirectionFromTube(old_element);
+
+    for (i = 0; i < NUM_DIRECTIONS; i++)
+    {
+      int xx = x + xy[i][0];
+      int yy = y + xy[i][1];
+
+      if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
+         IS_TUBE(IntelliDrawBuffer[last_x][last_y]))
+      {
+       int dir = MV_DIR_FROM_BIT(i);
+       int dir_opposite = MV_DIR_OPPOSITE(dir);
+       int last_element_old = IntelliDrawBuffer[last_x][last_y];
+       int last_direction_old = getOpenDirectionFromTube(last_element_old);
+       int last_direction_new = last_direction_old | dir_opposite;
+
+       last_element_new = getTubeFromOpenDirection(last_direction_new);
+
+       direction |= dir;
+      }
+    }
+
+    new_element = getTubeFromOpenDirectionNotEmpty(direction, new_element);
 
-    for (x = from_x; x <= to_x; x++)
-      DrawLineElement(x, y, element, change_level);
+    /* reduce connections of neighbour tube elements to minimal connections */
+    if (last_element_new != EL_UNDEFINED)
+    {
+      /* set neighbour tube elements to newly determined tube connections */
+      SetElementSimple(x, y, new_element, change_level);
+      SetElementSimple(last_x, last_y, last_element_new, change_level);
+
+      /* remove all open tube connections of neighbour tube elements */
+      new_element = getClosedTube(x, y);
+      last_element_new = getClosedTube(last_x, last_y);
+
+      /* set neighbour tube elements to new, minimized tube connections */
+      SetElementSimple(x, y, new_element, change_level);
+      SetElementSimple(last_x, last_y, last_element_new, change_level);
+    }
   }
-  else if (from_x == to_x)             /* vertical line */
+  else if (IS_ACID_POOL_OR_ACID(new_element))
+  {
+    static int xy[4][2] =
+    {
+      { -1, 0 },
+      { +1, 0 },
+      { 0, -1 },
+      { 0, +1 }
+    };
+    int last_element_new = EL_UNDEFINED;
+    int direction = MV_NONE;
+    int i;
+
+    /* if existing element is pool, keep all existing pool directions */
+    if (IS_ACID_POOL_OR_ACID(old_element))
+      direction |= getOpenDirectionFromPool(old_element);
+
+    for (i = 0; i < NUM_DIRECTIONS; i++)
+    {
+      int xx = x + xy[i][0];
+      int yy = y + xy[i][1];
+
+      if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
+         IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[last_x][last_y]))
+      {
+       int dir = MV_DIR_FROM_BIT(i);
+       int dir_opposite = MV_DIR_OPPOSITE(dir);
+       int last_element_old = IntelliDrawBuffer[last_x][last_y];
+       int last_direction_old = getOpenDirectionFromPool(last_element_old);
+       int last_direction_new = last_direction_old | dir_opposite;
+
+       last_element_new = getPoolFromOpenDirection(last_direction_new);
+
+       direction |= dir;
+      }
+    }
+
+#if 1
+    if (last_element_new == EL_EMPTY)
+      last_element_new = EL_ACID;
+#endif
+
+#if 1
+    if (last_element_new != EL_UNDEFINED)
+      new_element = getPoolFromOpenDirectionNotEmpty(direction,
+                                                    last_element_new);
+    else
+      new_element = getPoolFromOpenDirectionNotEmpty(direction, new_element);
+#else
+    new_element = getPoolFromOpenDirectionNotEmpty(direction, new_element);
+#endif
+
+    /* reduce connections of neighbour pool elements to minimal connections */
+    if (last_element_new != EL_UNDEFINED)
+    {
+      /* set neighbour pool elements to newly determined pool connections */
+      SetElementSimple(x, y, new_element, change_level);
+      SetElementSimple(last_x, last_y, last_element_new, change_level);
+
+      /* remove all open pool connections of neighbour pool elements */
+      new_element = getClosedPool(x, y);
+      last_element_new = getClosedPool(last_x, last_y);
+
+      /* set neighbour pool elements to new, minimized pool connections */
+      SetElementSimple(x, y, new_element, change_level);
+      SetElementSimple(last_x, last_y, last_element_new, change_level);
+    }
+  }
+  else if (IS_BELT(new_element))
   {
-    int x = from_x;
-    int y;
+    int last_element_new = EL_UNDEFINED;
 
-    if (from_y > to_y)
-      swap_numbers(&from_y, &to_y);
+    if (((last_x == x - 1 && last_y == y) ||
+        (last_x == x + 1 && last_y == y)) && IN_LEV_FIELD(last_x, last_y) &&
+       IS_BELT(IntelliDrawBuffer[last_x][last_y]))
+    {
+      int belt_nr = getBeltNrFromBeltElement(new_element);
+      int belt_middle = getBeltElementFromBeltNrAndBeltDir(belt_nr, MV_NONE);
+
+      new_element = belt_middle;
+      last_element_new = belt_middle;
+    }
+
+    /* reduce connections of neighbour belt elements to minimal connections */
+    if (last_element_new != EL_UNDEFINED)
+    {
+      /* set neighbour belt elements to newly determined belt connections */
+      SetElementSimple(x, y, new_element, change_level);
+      SetElementSimple(last_x, last_y, last_element_new, change_level);
 
-    for (y = from_y; y <= to_y; y++)
-      DrawLineElement(x, y, element, change_level);
+      /* remove all open belt connections of neighbour belt elements */
+      new_element = getClosedBelt(x, y);
+      last_element_new = getClosedBelt(last_x, last_y);
+
+      /* set neighbour belt elements to new, minimized belt connections */
+      SetElementSimple(x, y, new_element, change_level);
+      SetElementSimple(last_x, last_y, last_element_new, change_level);
+    }
   }
-  else                                 /* diagonal line */
+  else if (new_element == EL_EMC_WALL_1 ||
+          new_element == EL_EMC_WALL_2 ||
+          new_element == EL_EMC_WALL_3)
+  {
+    int last_element_new = EL_UNDEFINED;
+
+    if (last_x == x && last_y == y - 1 && IN_LEV_FIELD(last_x, last_y) &&
+       (IntelliDrawBuffer[last_x][last_y] == EL_EMC_WALL_1 ||
+        IntelliDrawBuffer[last_x][last_y] == EL_EMC_WALL_2 ||
+        IntelliDrawBuffer[last_x][last_y] == EL_EMC_WALL_3))
+    {
+      if (IN_LEV_FIELD(last_x, last_y - 1))
+      {
+       if (IntelliDrawBuffer[last_x][last_y - 1] != EL_EMC_WALL_1 &&
+           IntelliDrawBuffer[last_x][last_y - 1] != EL_EMC_WALL_2)
+         last_element_new = EL_EMC_WALL_1;
+       else if (IntelliDrawBuffer[last_x][last_y - 1] == EL_EMC_WALL_1 ||
+                IntelliDrawBuffer[last_x][last_y - 1] == EL_EMC_WALL_2)
+         last_element_new = EL_EMC_WALL_2;
+      }
+
+      new_element = EL_EMC_WALL_3;
+    }
+    else if (last_x == x && last_y == y + 1 && IN_LEV_FIELD(last_x, last_y) &&
+            (IntelliDrawBuffer[last_x][last_y] == EL_EMC_WALL_1 ||
+             IntelliDrawBuffer[last_x][last_y] == EL_EMC_WALL_2 ||
+             IntelliDrawBuffer[last_x][last_y] == EL_EMC_WALL_3))
+    {
+      if (IN_LEV_FIELD(last_x, last_y + 1))
+      {
+       if (IntelliDrawBuffer[last_x][last_y + 1] != EL_EMC_WALL_2 &&
+           IntelliDrawBuffer[last_x][last_y + 1] != EL_EMC_WALL_3)
+         last_element_new = EL_EMC_WALL_3;
+       else if (IntelliDrawBuffer[last_x][last_y + 1] == EL_EMC_WALL_2 ||
+                IntelliDrawBuffer[last_x][last_y + 1] == EL_EMC_WALL_3)
+         last_element_new = EL_EMC_WALL_2;
+      }
+
+      new_element = EL_EMC_WALL_1;
+    }
+
+    if (last_element_new != EL_UNDEFINED)
+      SetElementSimple(last_x, last_y, last_element_new, change_level);
+  }
+  else if (IS_BELT_SWITCH(old_element))
   {
-    int len_x = ABS(to_x - from_x);
-    int len_y = ABS(to_y - from_y);
-    int x, y;
+    int belt_nr = getBeltNrFromBeltSwitchElement(old_element);
+    int belt_dir = getBeltDirFromBeltSwitchElement(old_element);
 
-    if (len_y < len_x)                 /* a < 1 */
+    belt_dir = (belt_dir == MV_LEFT ? MV_NONE :
+               belt_dir == MV_NONE ? MV_RIGHT : MV_LEFT);
+
+    new_element = getBeltSwitchElementFromBeltNrAndBeltDir(belt_nr, belt_dir);
+  }
+  else
+  {
+    static int swappable_elements[][2] =
     {
-      float a = (float)len_y / (float)len_x;
+      { EL_EXIT_CLOSED,                        EL_EXIT_OPEN                    },
+      { EL_DYNAMITE,                   EL_DYNAMITE_ACTIVE              },
+      { EL_EM_DYNAMITE,                        EL_EM_DYNAMITE_ACTIVE           },
+      { EL_QUICKSAND_EMPTY,            EL_QUICKSAND_FULL               },
+      { EL_EMERALD,                    EL_WALL_EMERALD                 },
+      { EL_EMERALD_YELLOW,             EL_WALL_EMERALD_YELLOW          },
+      { EL_EMERALD_RED,                        EL_WALL_EMERALD_RED             },
+      { EL_EMERALD_PURPLE,             EL_WALL_EMERALD_PURPLE          },
+      { EL_DIAMOND,                    EL_WALL_DIAMOND                 },
+      { EL_BD_DIAMOND,                 EL_WALL_BD_DIAMOND              },
+      { EL_GATE_1,                     EL_GATE_1_GRAY                  },
+      { EL_GATE_2,                     EL_GATE_2_GRAY                  },
+      { EL_GATE_3,                     EL_GATE_3_GRAY                  },
+      { EL_GATE_4,                     EL_GATE_4_GRAY                  },
+      { EL_EM_GATE_1,                  EL_EM_GATE_1_GRAY               },
+      { EL_EM_GATE_2,                  EL_EM_GATE_2_GRAY               },
+      { EL_EM_GATE_3,                  EL_EM_GATE_3_GRAY               },
+      { EL_EM_GATE_4,                  EL_EM_GATE_4_GRAY               },
+      { EL_EMC_GATE_5,                 EL_EMC_GATE_5_GRAY              },
+      { EL_EMC_GATE_6,                 EL_EMC_GATE_6_GRAY              },
+      { EL_EMC_GATE_7,                 EL_EMC_GATE_7_GRAY              },
+      { EL_EMC_GATE_8,                 EL_EMC_GATE_8_GRAY              },
+      { EL_DC_GATE_WHITE,              EL_DC_GATE_WHITE_GRAY           },
+      { EL_TIME_ORB_EMPTY,             EL_TIME_ORB_FULL                },
+      { EL_LAMP,                       EL_LAMP_ACTIVE                  },
+      { EL_SOKOBAN_FIELD_EMPTY,                EL_SOKOBAN_FIELD_FULL           },
+      { EL_SP_BASE,                    EL_SP_BUGGY_BASE                },
+      { EL_PEARL,                      EL_WALL_PEARL                   },
+      { EL_CRYSTAL,                    EL_WALL_CRYSTAL                 },
+      { EL_TIMEGATE_CLOSED,            EL_TIMEGATE_OPEN                },
+      { EL_SWITCHGATE_CLOSED,          EL_SWITCHGATE_OPEN              },
+      { EL_SWITCHGATE_SWITCH_UP,       EL_SWITCHGATE_SWITCH_DOWN       },
+      { EL_DC_SWITCHGATE_SWITCH_UP,    EL_DC_SWITCHGATE_SWITCH_DOWN    },
+      { EL_LIGHT_SWITCH,               EL_LIGHT_SWITCH_ACTIVE          },
+      { EL_LANDMINE,                   EL_DC_LANDMINE                  },
+      { EL_SHIELD_NORMAL,              EL_SHIELD_DEADLY                },
+      { EL_STEEL_EXIT_CLOSED,          EL_STEEL_EXIT_OPEN              },
+      { EL_EM_EXIT_CLOSED,             EL_EM_EXIT_OPEN                 },
+      { EL_EM_STEEL_EXIT_CLOSED,       EL_EM_STEEL_EXIT_OPEN           },
+      { EL_QUICKSAND_FAST_EMPTY,       EL_QUICKSAND_FAST_FULL          },
+
+      { -1,                            -1                              },
+    };
+    int i;
+
+    for (i = 0; swappable_elements[i][0] != -1; i++)
+    {
+      int element1 = swappable_elements[i][0];
+      int element2 = swappable_elements[i][1];
+
+      if (old_element == element1 || old_element == element2)
+       new_element = (old_element == element1 ? element2 : element1);
+    }
+  }
+
+  SetElementSimple(x, y, new_element, change_level);
+
+  last_x = x;
+  last_y = y;
+}
 
-      if (from_x > to_x)
-       swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
+static void ResetIntelliDraw()
+{
+  int x, y;
+
+  for (x = 0; x < lev_fieldx; x++)
+    for (y = 0; y < lev_fieldy; y++)
+      IntelliDrawBuffer[x][y] = Feld[x][y];
+
+  SetElementIntelliDraw(-1, -1, EL_UNDEFINED, FALSE);
+}
+
+static void SetElementExt(int x, int y, int element, boolean change_level)
+{
+  if (element < 0)
+    SetElementSimple(x, y, Feld[x][y], change_level);
+  else if (GetKeyModState() & KMOD_Shift)
+    SetElementIntelliDraw(x, y, element, change_level);
+  else
+    SetElementSimple(x, y, element, change_level);
+}
+
+static void SetElement(int x, int y, int element)
+{
+  SetElementExt(x, y, element, TRUE);
+}
 
-      for (x = 0; x <= len_x; x++)
+static void DrawLineElement(int sx, int sy, int element, boolean change_level)
+{
+  int lx = sx + level_xpos;
+  int ly = sy + level_ypos;
+
+  SetElementExt(lx, ly, element, change_level);
+}
+
+static void DrawLine(int from_x, int from_y, int to_x, int to_y,
+                    int element, boolean change_level)
+{
+  int xsize = ABS(to_x - from_x);
+  int ysize = ABS(to_y - from_y);
+  int dx = (to_x < from_x ? -1 : +1);
+  int dy = (to_y < from_y ? -1 : +1);
+  int i;
+
+  if (from_y == to_y)                  /* horizontal line */
+  {
+    for (i = 0; i <= xsize; i++)
+      DrawLineElement(from_x + i * dx, from_y, element, change_level);
+  }
+  else if (from_x == to_x)             /* vertical line */
+  {
+    for (i = 0; i <= ysize; i++)
+      DrawLineElement(from_x, from_y + i * dy, element, change_level);
+  }
+  else                                 /* diagonal line */
+  {
+    if (ysize < xsize)                 /* a < 1 */
+    {
+      float a = (float)ysize / (float)xsize;
+
+      for (i = 0; i <= xsize; i++)
       {
-       y = (int)(a * x + 0.5) * (to_y < from_y ? -1 : +1);
+       int x = dx * i;
+       int y = dy * (int)(a * i + 0.5);
+
        DrawLineElement(from_x + x, from_y + y, element, change_level);
       }
     }
     else                               /* a >= 1 */
     {
-      float a = (float)len_x / (float)len_y;
+      float a = (float)xsize / (float)ysize;
 
-      if (from_y > to_y)
-       swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
-
-      for (y = 0; y <= len_y; y++)
+      for (i = 0; i <= ysize; i++)
       {
-       x = (int)(a * y + 0.5) * (to_x < from_x ? -1 : +1);
+       int x = dx * (int)(a * i + 0.5);
+       int y = dy * i;
+
        DrawLineElement(from_x + x, from_y + y, element, change_level);
       }
     }
@@ -8617,7 +9155,7 @@ static void CopyBrushExt(int from_x, int from_y, int to_x, int to_y,
 
     for (y = 0; y < brush_height; y++)
     {
-      for (x=0; x < brush_width; x++)
+      for (x = 0; x < brush_width; x++)
       {
        brush_buffer[x][y] = Feld[from_lx + x][from_ly + y];
 
@@ -9047,6 +9585,12 @@ static void HandleDrawingAreas(struct GadgetInfo *gi)
     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
   }
 
+  if (GetKeyModState() & KMOD_Shift)
+  {
+    if (button_press_event || button_release_event)
+      ResetIntelliDraw();
+  }
+
   switch (actual_drawing_function)
   {
     case GADGET_ID_SINGLE_ITEMS:
@@ -9059,17 +9603,27 @@ static void HandleDrawingAreas(struct GadgetInfo *gi)
          if (edit_mode == ED_MODE_DRAWING && draw_with_brush &&
              !inside_drawing_area)
            DeleteBrushFromCursor();
+
+#if 0
+         ResetIntelliDraw();
+#endif
        }
 
-       if (!button)
+       if (!button || button_release_event)
          break;
 
        if (draw_with_brush)
        {
+#if 0
          if (!button_release_event)
+#endif
            CopyBrushToLevel(sx, sy, button);
        }
+#if 1
+       else
+#else
        else if (new_element != Feld[lx][ly])
+#endif
        {
          if (new_element == EL_PLAYER_1)
          {
@@ -9080,18 +9634,30 @@ static void HandleDrawingAreas(struct GadgetInfo *gi)
              {
                if (Feld[x][y] == EL_PLAYER_1)
                {
+#if 1
+                 SetElement(x, y, EL_EMPTY);
+#else
                  Feld[x][y] = EL_EMPTY;
+#if 1
+                 if (IN_ED_FIELD(x - level_xpos, y - level_ypos))
+                   DrawMiniElement(x - level_xpos, y - level_ypos, EL_EMPTY);
+#else
                  if (x - level_xpos >= 0 && x - level_xpos < ed_fieldx &&
                      y - level_ypos >= 0 && y - level_ypos < ed_fieldy)
-                   DrawMiniElement(x - level_xpos, y - level_ypos,
-                                   EL_EMPTY);
+                   DrawMiniElement(x - level_xpos, y - level_ypos, EL_EMPTY);
+#endif
+#endif
                }
              }
            }
          }
 
+#if 1
+         SetElement(lx, ly, new_element);
+#else
          Feld[lx][ly] = new_element;
          DrawMiniElement(sx, sy, new_element);
+#endif
        }
       }
       else