+static int getDirectionFromTube(int element)
+{
+ 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 getTubeFromDirection(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 getTubeFromDirectionNotEmpty(int direction, int element_old)
+{
+ int element_new = getTubeFromDirection(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 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 = getDirectionFromTube(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) &&
+ (getDirectionFromTube(IntelliDrawBuffer[xx][yy]) & dir_opposite))
+ tube_direction_new |= dir;
+ }
+
+ return getTubeFromDirectionNotEmpty(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 void SetElementSimple(int x, int y, int element, boolean change_level)
+{
+ if (change_level)
+ Feld[x][y] = element;
+
+ IntelliDrawBuffer[x][y] = element;
+
+ if (IN_ED_FIELD(x - level_xpos, y - level_ypos))
+ DrawMiniElement(x - level_xpos, y - level_ypos, element);
+}
+
+static void SetElementIntelliDraw(int x, int y, int new_element,
+ boolean change_level)
+{
+ static int last_x = -1;
+ static int last_y = -1;
+ int old_element = IntelliDrawBuffer[x][y];
+
+ if (new_element == EL_UNDEFINED)
+ {
+ last_x = -1;
+ last_y = -1;
+
+ return;
+ }
+
+ if (IS_TUBE(new_element))
+ {
+ static int xy[4][2] =
+ {
+ { -1, 0 },
+ { +1, 0 },
+ { 0, -1 },
+ { 0, +1 }
+ };
+ boolean last_element_is_neighbour = FALSE;
+ int last_element_new;
+ int direction = MV_NONE;
+ int i;
+
+ /* if existing element is tube, keep all existing tube directions */
+ if (IS_TUBE(old_element))
+ direction |= getDirectionFromTube(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 = getDirectionFromTube(last_element_old);
+ int last_direction_new = last_direction_old | dir_opposite;
+
+ last_element_new = getTubeFromDirection(last_direction_new);
+ last_element_is_neighbour = TRUE;
+
+ direction |= dir;
+ }
+ }
+
+ new_element = getTubeFromDirectionNotEmpty(direction, new_element);
+
+ /* reduce connections of neighbour tube elements to minimal connections */
+ if (last_element_is_neighbour)
+ {
+ /* 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 (IS_ACID_POOL(new_element))
+ {
+ int last_element_new = EL_UNDEFINED;
+
+ if (IS_ACID_POOL(old_element))
+ {
+ new_element = old_element;
+ }
+
+ if (last_x == x - 1 && last_y == y && IN_LEV_FIELD(last_x, last_y) &&
+ IS_ACID_POOL(IntelliDrawBuffer[last_x][last_y]))
+ {
+ if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_TOPLEFT)
+ {
+ new_element = EL_ACID_POOL_TOPRIGHT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_TOPRIGHT)
+ {
+ last_element_new = EL_ACID;
+ new_element = EL_ACID_POOL_TOPRIGHT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_BOTTOMLEFT)
+ {
+ new_element = EL_ACID_POOL_BOTTOMRIGHT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_BOTTOMRIGHT)
+ {
+ last_element_new = EL_ACID_POOL_BOTTOM;
+ new_element = EL_ACID_POOL_BOTTOMRIGHT;
+ }
+ }
+ else if (last_x == x + 1 && last_y == y && IN_LEV_FIELD(last_x, last_y) &&
+ IS_ACID_POOL(IntelliDrawBuffer[last_x][last_y]))
+ {
+ if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_TOPLEFT)
+ {
+ last_element_new = EL_ACID;
+ new_element = EL_ACID_POOL_TOPLEFT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_TOPRIGHT)
+ {
+ new_element = EL_ACID_POOL_TOPLEFT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_BOTTOMLEFT)
+ {
+ last_element_new = EL_ACID_POOL_BOTTOM;
+ new_element = EL_ACID_POOL_BOTTOMLEFT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_BOTTOMRIGHT)
+ {
+ new_element = EL_ACID_POOL_BOTTOMLEFT;
+ }
+ }
+ else if (last_x == x && last_y == y - 1 && IN_LEV_FIELD(last_x, last_y) &&
+ IS_ACID_POOL(IntelliDrawBuffer[last_x][last_y]))
+ {
+ if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_TOPLEFT)
+ {
+ new_element = EL_ACID_POOL_BOTTOMLEFT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_TOPRIGHT)
+ {
+ new_element = EL_ACID_POOL_BOTTOMRIGHT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_BOTTOMLEFT)
+ {
+ last_element_new = EL_ACID_POOL_TOPLEFT;
+ new_element = EL_ACID_POOL_BOTTOMLEFT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_BOTTOMRIGHT)
+ {
+ last_element_new = EL_ACID_POOL_TOPRIGHT;
+ new_element = EL_ACID_POOL_BOTTOMRIGHT;
+ }
+ else
+ {
+ last_element_new = EL_ACID;
+ new_element = EL_ACID_POOL_BOTTOM;
+ }
+ }
+ else if (last_x == x && last_y == y + 1 && IN_LEV_FIELD(last_x, last_y) &&
+ IS_ACID_POOL(IntelliDrawBuffer[last_x][last_y]))
+ {
+ if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_TOPLEFT)
+ {
+ new_element = EL_ACID_POOL_TOPLEFT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_TOPRIGHT)
+ {
+ new_element = EL_ACID_POOL_TOPRIGHT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_BOTTOMLEFT)
+ {
+ new_element = EL_ACID_POOL_TOPLEFT;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == EL_ACID_POOL_BOTTOMRIGHT)
+ {
+ new_element = EL_ACID_POOL_TOPRIGHT;
+ }
+ else
+ {
+ last_element_new = EL_ACID;
+ new_element = EL_ACID;
+ }
+ }
+
+ if (last_element_new != EL_UNDEFINED)
+ SetElementSimple(last_x, last_y, last_element_new, change_level);
+ }
+ else if (IS_BELT(new_element))
+ {
+ int last_element_new = EL_UNDEFINED;
+ int belt_nr = getBeltNrFromBeltElement(new_element);
+#if 0
+ int belt_left = getBeltElementFromBeltNrAndBeltDir(belt_nr, MV_LEFT);
+#endif
+ int belt_middle = getBeltElementFromBeltNrAndBeltDir(belt_nr, MV_NONE);
+#if 0
+ int belt_right = getBeltElementFromBeltNrAndBeltDir(belt_nr, MV_RIGHT);
+#endif
+ boolean last_element_is_neighbour = FALSE;
+
+#if 0
+ if (IS_BELT(old_element))
+ {
+ new_element = old_element;
+ }
+#endif
+
+ if (last_x == x - 1 && last_y == y && IN_LEV_FIELD(last_x, last_y) &&
+ IS_BELT(IntelliDrawBuffer[last_x][last_y]))
+ {
+ last_element_new = IntelliDrawBuffer[last_x][last_y];
+
+#if 1
+ last_element_new = belt_middle;
+ new_element = belt_middle;
+#else
+ if (IntelliDrawBuffer[last_x][last_y] == belt_left)
+ {
+ new_element = belt_right;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == belt_right)
+ {
+ last_element_new = belt_middle;
+ new_element = belt_right;
+ }
+#endif
+
+ last_element_is_neighbour = TRUE;
+ }
+ else if (last_x == x + 1 && last_y == y && IN_LEV_FIELD(last_x, last_y) &&
+ IS_BELT(IntelliDrawBuffer[last_x][last_y]))
+ {
+ last_element_new = IntelliDrawBuffer[last_x][last_y];
+
+#if 1
+ last_element_new = belt_middle;
+ new_element = belt_middle;
+#else
+ if (IntelliDrawBuffer[last_x][last_y] == belt_left)
+ {
+ last_element_new = belt_middle;
+ new_element = belt_left;
+ }
+ else if (IntelliDrawBuffer[last_x][last_y] == belt_right)
+ {
+ new_element = belt_left;
+ }
+#endif
+
+ last_element_is_neighbour = TRUE;
+ }
+
+ if (last_element_new != EL_UNDEFINED)
+ SetElementSimple(last_x, last_y, last_element_new, change_level);
+
+ /* reduce connections of neighbour belt elements to minimal connections */
+ if (last_element_is_neighbour)
+ {
+ /* 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);
+
+ /* 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 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 belt_nr = getBeltNrFromBeltSwitchElement(old_element);
+ int belt_dir = getBeltDirFromBeltSwitchElement(old_element);
+
+ 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] =
+ {
+ { 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;
+}
+
+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)
+ {
+ element = IntelliDrawBuffer[x][y] = Feld[x][y];
+ SetElementSimple(x, y, element, change_level);
+
+ return;
+ }
+
+ 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);
+}
+