tron@2186: /* $Id$ */ tron@2186: belugas@6928: /** @file water_cmd.cpp */ belugas@6928: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@3189: #include "bridge_map.h" rubidium@6486: #include "bridge.h" tron@6460: #include "cmd_helper.h" tron@3338: #include "station_map.h" rubidium@8615: #include "tile_cmd.h" maedhros@6949: #include "landscape.h" rubidium@8720: #include "viewport_func.h" rubidium@8612: #include "command_func.h" truelight@0: #include "town.h" darkvater@149: #include "news.h" truelight@1313: #include "depot.h" matthijs@1752: #include "vehicle_gui.h" bjarni@2676: #include "train.h" maedhros@7353: #include "roadveh.h" rubidium@8604: #include "water.h" tron@3111: #include "water_map.h" rubidium@8003: #include "industry_map.h" peter1138@5210: #include "newgrf.h" peter1138@7079: #include "newgrf_canal.h" rubidium@7885: #include "misc/autoptr.hpp" belugas@8345: #include "transparency.h" rubidium@8610: #include "strings_func.h" rubidium@8627: #include "functions.h" rubidium@8627: #include "window_func.h" rubidium@8640: #include "vehicle_func.h" rubidium@8653: #include "sound_func.h" rubidium@8707: #include "variables.h" rubidium@8750: #include "player_func.h" rubidium@8766: #include "settings_type.h" frosch@8876: #include "clear_map.h" frosch@8955: #include "tree_map.h" darkvater@149: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" ludde@2261: frosch@8876: /** frosch@8876: * Describes the behaviour of a tile during flooding. frosch@8876: */ frosch@8876: enum FloodingBehaviour { frosch@8876: FLOOD_NONE, ///< The tile does not flood neighboured tiles. frosch@8876: FLOOD_ACTIVE, ///< The tile floods neighboured tiles. frosch@8876: FLOOD_PASSIVE, ///< The tile does not actively flood neighboured tiles, but it prevents them from drying up. frosch@8876: FLOOD_DRYUP, ///< The tile drys up if it is not constantly flooded from neighboured tiles. frosch@8876: }; frosch@8876: frosch@8876: /** frosch@8876: * Describes from which directions a specific slope can be flooded (if the tile is floodable at all). frosch@8876: */ frosch@8876: static const uint8 _flood_from_dirs[] = { frosch@8876: (1 << DIR_NW) | (1 << DIR_SW) | (1 << DIR_SE) | (1 << DIR_NE), // SLOPE_FLAT frosch@8876: (1 << DIR_NE) | (1 << DIR_SE), // SLOPE_W frosch@8876: (1 << DIR_NW) | (1 << DIR_NE), // SLOPE_S frosch@8876: (1 << DIR_NE), // SLOPE_SW frosch@8876: (1 << DIR_NW) | (1 << DIR_SW), // SLOPE_E frosch@8876: 0, // SLOPE_EW frosch@8876: (1 << DIR_NW), // SLOPE_SE frosch@8876: (1 << DIR_N ) | (1 << DIR_NW) | (1 << DIR_NE), // SLOPE_WSE, SLOPE_STEEP_S frosch@8876: (1 << DIR_SW) | (1 << DIR_SE), // SLOPE_N frosch@8876: (1 << DIR_SE), // SLOPE_NW frosch@8876: 0, // SLOPE_NS frosch@8876: (1 << DIR_E ) | (1 << DIR_NE) | (1 << DIR_SE), // SLOPE_NWS, SLOPE_STEEP_W frosch@8876: (1 << DIR_SW), // SLOPE_NE frosch@8876: (1 << DIR_S ) | (1 << DIR_SW) | (1 << DIR_SE), // SLOPE_ENW, SLOPE_STEEP_N frosch@8876: (1 << DIR_W ) | (1 << DIR_SW) | (1 << DIR_NW), // SLOPE_SEN, SLOPE_STEEP_E frosch@8876: }; frosch@8876: frosch@8876: /** frosch@8939: * Marks tile dirty if it is a canal or river tile. frosch@8939: * Called to avoid glitches when flooding tiles next to canal tile. frosch@8939: * frosch@8939: * @param tile tile to check frosch@8939: */ frosch@8939: static inline void MarkTileDirtyIfCanalOrRiver(TileIndex tile) frosch@8939: { frosch@8939: if (IsTileType(tile, MP_WATER) && (IsCanal(tile) || IsRiver(tile))) MarkTileDirtyByTile(tile); frosch@8939: } frosch@8939: frosch@8939: /** frosch@8939: * Marks the tiles around a tile as dirty, if they are canals or rivers. frosch@8939: * frosch@8939: * @param tile The center of the tile where all other tiles are marked as dirty frosch@8939: * @ingroup dirty frosch@8939: */ frosch@8939: static void MarkCanalsAndRiversAroundDirty(TileIndex tile) frosch@8939: { frosch@8939: for (Direction dir = DIR_BEGIN; dir < DIR_END; dir++) { frosch@8939: MarkTileDirtyIfCanalOrRiver(tile + TileOffsByDir(dir)); frosch@8939: } frosch@8939: } frosch@8939: frosch@8939: /** rubidium@8444: * Makes a tile canal or water depending on the surroundings. frosch@8991: * frosch@8991: * Must only be used for converting old savegames. Use WaterClass now. frosch@8991: * rubidium@8444: * This as for example docks and shipdepots do not store rubidium@8444: * whether the tile used to be canal or 'normal' water. rubidium@8444: * @param t the tile to change. rubidium@8444: * @param o the owner of the new tile. rubidium@8444: */ peter1138@8967: void SetWaterClassDependingOnSurroundings(TileIndex t) rubidium@8444: { rubidium@8444: assert(GetTileSlope(t, NULL) == SLOPE_FLAT); rubidium@8444: smatz@8518: /* Mark tile dirty in all cases */ smatz@8518: MarkTileDirtyByTile(t); smatz@8518: smatz@9021: if (TileX(t) == 0 || TileY(t) == 0 || TileX(t) == MapMaxX() || TileY(t) == MapMaxY()) { smatz@9021: /* tiles at map borders are always WATER_CLASS_SEA */ smatz@9021: SetWaterClass(t, WATER_CLASS_SEA); smatz@9021: return; smatz@9021: } smatz@9021: rubidium@8444: bool has_water = false; rubidium@8444: bool has_canal = false; peter1138@8967: bool has_river = false; rubidium@8444: rubidium@8444: for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) { rubidium@8444: TileIndex neighbour = TileAddByDiagDir(t, dir); frosch@8910: switch (GetTileType(neighbour)) { frosch@8910: case MP_WATER: frosch@8991: /* clear water and shipdepots have already a WaterClass associated */ frosch@8991: if (IsCoast(neighbour)) { frosch@8991: has_water = true; frosch@8991: } else if (!IsLock(neighbour)) { frosch@8991: switch (GetWaterClass(neighbour)) { frosch@8991: case WATER_CLASS_SEA: has_water = true; break; frosch@8991: case WATER_CLASS_CANAL: has_canal = true; break; frosch@8991: case WATER_CLASS_RIVER: has_river = true; break; frosch@8991: default: NOT_REACHED(); frosch@8991: } frosch@8991: } frosch@8910: break; frosch@8910: frosch@8910: case MP_RAILWAY: frosch@8910: /* Shore or flooded halftile */ frosch@8910: has_water |= (GetRailGroundType(neighbour) == RAIL_GROUND_WATER); frosch@8910: break; frosch@8910: frosch@8955: case MP_TREES: frosch@8955: /* trees on shore */ frosch@8955: has_water |= (GetTreeGround(neighbour) == TREE_GROUND_SHORE); frosch@8955: break; frosch@8955: frosch@8910: default: break; rubidium@8444: } rubidium@8444: } peter1138@8967: peter1138@8967: if (has_river && !has_canal) { peter1138@8967: SetWaterClass(t, WATER_CLASS_RIVER); peter1138@8967: } else if (has_canal || !has_water) { peter1138@8967: SetWaterClass(t, WATER_CLASS_CANAL); rubidium@8444: } else { peter1138@8967: SetWaterClass(t, WATER_CLASS_SEA); rubidium@8444: } rubidium@8444: } rubidium@8444: rubidium@8444: Darkvater@1784: /** Build a ship depot. tron@3491: * @param tile tile where ship depot is built belugas@6928: * @param flags type of operation tron@6460: * @param p1 bit 0 depot orientation (Axis) Darkvater@1784: * @param p2 unused truelight@0: */ rubidium@7439: CommandCost CmdBuildShipDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@3491: TileIndex tile2; truelight@193: rubidium@8726: CommandCost ret; truelight@0: tron@6460: Axis axis = Extract(p1); tron@6460: tron@6460: tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); truelight@0: peter1138@8967: if (!IsWaterTile(tile) || !IsWaterTile(tile2)) { truelight@0: return_cmd_error(STR_3801_MUST_BE_BUILT_ON_WATER); peter1138@8967: } truelight@0: celestar@5573: if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5573: peter1138@8967: if (GetTileSlope(tile, NULL) != SLOPE_FLAT || GetTileSlope(tile2, NULL) != SLOPE_FLAT) { peter1138@8967: /* Prevent depots on rapids */ peter1138@8967: return_cmd_error(STR_0239_SITE_UNSUITABLE); peter1138@8967: } peter1138@8967: peter1138@8967: WaterClass wc1 = GetWaterClass(tile); peter1138@8967: WaterClass wc2 = GetWaterClass(tile2); tron@3491: ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); Darkvater@1784: if (CmdFailed(ret)) return CMD_ERROR; tron@3491: ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR); Darkvater@1784: if (CmdFailed(ret)) return CMD_ERROR; truelight@193: rubidium@7885: Depot *depot = new Depot(tile); Darkvater@1784: if (depot == NULL) return CMD_ERROR; rubidium@7885: AutoPtrT d_auto_delete = depot; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@1313: depot->town_index = ClosestTownFromTile(tile, (uint)-1)->index; truelight@0: frosch@8991: MakeShipDepot(tile, _current_player, DEPOT_NORTH, axis, wc1); frosch@8991: MakeShipDepot(tile2, _current_player, DEPOT_SOUTH, axis, wc2); celestar@3372: MarkTileDirtyByTile(tile); celestar@3372: MarkTileDirtyByTile(tile2); rubidium@7885: d_auto_delete.Detach(); truelight@0: } truelight@0: rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.build_ship_depot); truelight@0: } truelight@0: peter1138@8967: void MakeWaterKeepingClass(TileIndex tile, Owner o) rubidium@8525: { peter1138@8967: assert(IsTileType(tile, MP_WATER) || (IsTileType(tile, MP_STATION) && (IsBuoy(tile) || IsDock(tile)))); peter1138@8967: peter1138@8967: switch (GetWaterClass(tile)) { peter1138@8967: case WATER_CLASS_SEA: MakeWater(tile); break; peter1138@8967: case WATER_CLASS_CANAL: MakeCanal(tile, o, Random()); break; peter1138@8967: case WATER_CLASS_RIVER: MakeRiver(tile, Random()); break; rubidium@8525: } rubidium@8525: } rubidium@8525: rubidium@7439: static CommandCost RemoveShipDepot(TileIndex tile, uint32 flags) truelight@0: { celestar@3373: if (!IsShipDepot(tile)) return CMD_ERROR; tron@2951: if (!CheckTileOwnership(tile)) return CMD_ERROR; truelight@0: smatz@9016: TileIndex tile2 = GetOtherShipDepotTile(tile); truelight@0: smatz@9016: /* do not check for ship on tile when company goes bankrupt */ smatz@9016: if (!(flags & DC_BANKRUPT)) { smatz@9016: if (!EnsureNoVehicleOnGround(tile) || !EnsureNoVehicleOnGround(tile2)) return CMD_ERROR; smatz@9016: } truelight@0: truelight@0: if (flags & DC_EXEC) { celestar@3373: /* Kill the depot, which is registered at the northernmost tile. Use that one */ rubidium@7885: delete GetDepotByTile(tile2 < tile ? tile2 : tile); truelight@0: frosch@8991: MakeWaterKeepingClass(tile, GetTileOwner(tile)); frosch@8991: MakeWaterKeepingClass(tile2, GetTileOwner(tile2)); tron@3111: MarkTileDirtyByTile(tile); tron@3111: MarkTileDirtyByTile(tile2); truelight@0: } truelight@0: rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_ship_depot); truelight@0: } truelight@0: belugas@6928: /** build a shiplift */ rubidium@7439: static CommandCost DoBuildShiplift(TileIndex tile, DiagDirection dir, uint32 flags) truelight@0: { rubidium@7439: CommandCost ret; truelight@0: int delta; truelight@0: belugas@6928: /* middle tile */ tron@3491: ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); peter1138@2737: if (CmdFailed(ret)) return CMD_ERROR; truelight@193: Darkvater@4559: delta = TileOffsByDiagDir(dir); belugas@6928: /* lower tile */ peter1138@8967: WaterClass wc_lower = IsWaterTile(tile - delta) ? GetWaterClass(tile - delta) : WATER_CLASS_CANAL; peter1138@8967: tron@3491: ret = DoCommand(tile - delta, 0, 0, flags, CMD_LANDSCAPE_CLEAR); peter1138@2737: if (CmdFailed(ret)) return CMD_ERROR; tron@3636: if (GetTileSlope(tile - delta, NULL) != SLOPE_FLAT) { tron@3636: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); tron@3636: } truelight@0: belugas@6928: /* upper tile */ peter1138@8967: WaterClass wc_upper = IsWaterTile(tile + delta) ? GetWaterClass(tile + delta) : WATER_CLASS_CANAL; peter1138@8967: tron@3491: ret = DoCommand(tile + delta, 0, 0, flags, CMD_LANDSCAPE_CLEAR); peter1138@2737: if (CmdFailed(ret)) return CMD_ERROR; tron@3636: if (GetTileSlope(tile + delta, NULL) != SLOPE_FLAT) { tron@3636: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); tron@3636: } truelight@0: celestar@5573: if ((MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) || celestar@5573: (MayHaveBridgeAbove(tile - delta) && IsBridgeAbove(tile - delta)) || celestar@5573: (MayHaveBridgeAbove(tile + delta) && IsBridgeAbove(tile + delta))) { celestar@5573: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5573: } celestar@5573: truelight@0: if (flags & DC_EXEC) { peter1138@8967: MakeLock(tile, _current_player, dir, wc_lower, wc_upper); celestar@3372: MarkTileDirtyByTile(tile); celestar@3372: MarkTileDirtyByTile(tile - delta); celestar@3372: MarkTileDirtyByTile(tile + delta); frosch@8939: MarkCanalsAndRiversAroundDirty(tile - delta); frosch@8939: MarkCanalsAndRiversAroundDirty(tile + delta); truelight@0: } truelight@0: rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_water * 22 >> 3); truelight@0: } truelight@0: rubidium@7439: static CommandCost RemoveShiplift(TileIndex tile, uint32 flags) truelight@0: { Darkvater@4559: TileIndexDiff delta = TileOffsByDiagDir(GetLockDirection(tile)); truelight@0: rubidium@7766: if (!CheckTileOwnership(tile) && GetTileOwner(tile) != OWNER_NONE) return CMD_ERROR; peter1138@3940: belugas@6928: /* make sure no vehicle is on the tile. */ rubidium@8254: if (!EnsureNoVehicleOnGround(tile) || !EnsureNoVehicleOnGround(tile + delta) || !EnsureNoVehicleOnGround(tile - delta)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: DoClearSquare(tile); peter1138@8967: MakeWaterKeepingClass(tile + delta, GetTileOwner(tile)); peter1138@8967: MakeWaterKeepingClass(tile - delta, GetTileOwner(tile)); peter1138@8967: MarkTileDirtyByTile(tile - delta); peter1138@8967: MarkTileDirtyByTile(tile + delta); frosch@8939: MarkCanalsAndRiversAroundDirty(tile - delta); frosch@8939: MarkCanalsAndRiversAroundDirty(tile + delta); truelight@0: } truelight@193: rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_water * 2); truelight@0: } truelight@0: Darkvater@1796: /** Builds a lock (ship-lift) tron@3491: * @param tile tile where to place the lock belugas@6928: * @param flags type of operation Darkvater@1796: * @param p1 unused Darkvater@1796: * @param p2 unused Darkvater@1796: */ rubidium@7439: CommandCost CmdBuildLock(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { frosch@8909: DiagDirection dir = GetInclinedSlopeDirection(GetTileSlope(tile, NULL)); frosch@8909: if (dir == INVALID_DIAGDIR) return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); peter1138@8882: peter1138@8882: /* Disallow building of locks on river rapids */ peter1138@8967: if (IsWaterTile(tile)) return_cmd_error(STR_0239_SITE_UNSUITABLE); peter1138@8882: tron@3157: return DoBuildShiplift(tile, dir, flags); truelight@0: } truelight@0: Darkvater@1796: /** Build a piece of canal. tron@3491: * @param tile end tile of stretch-dragging belugas@6928: * @param flags type of operation Darkvater@1796: * @param p1 start tile of stretch-dragging frosch@8909: * @param p2 specifies canal (0), water (1) or river (2); last two can only be built in scenario editor Darkvater@1796: */ rubidium@7439: CommandCost CmdBuildCanal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@8726: CommandCost cost(EXPENSES_CONSTRUCTION); Darkvater@1632: int size_x, size_y; tron@3491: int x; tron@3491: int y; Darkvater@1796: int sx, sy; Darkvater@1796: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; peter1138@8856: rubidium@6612: /* Outside of the editor you can only build canals, not oceans */ peter1138@8857: if (p2 != 0 && _game_mode != GM_EDITOR) return CMD_ERROR; Darkvater@1796: tron@3491: x = TileX(tile); tron@3491: y = TileY(tile); Darkvater@1796: sx = TileX(p1); Darkvater@1796: sy = TileY(p1); Darkvater@1632: tron@6432: if (x < sx) Swap(x, sx); tron@6432: if (y < sy) Swap(y, sy); Darkvater@1632: size_x = (x - sx) + 1; Darkvater@1632: size_y = (y - sy) + 1; truelight@193: Darkvater@1796: /* Outside the editor you can only drag canals, and not areas */ Darkvater@1796: if (_game_mode != GM_EDITOR && (sx != x && sy != y)) return CMD_ERROR; Darkvater@1796: tron@1981: BEGIN_TILE_LOOP(tile, size_x, size_y, TileXY(sx, sy)) { rubidium@7439: CommandCost ret; celestar@5573: peter1138@8856: Slope slope = GetTileSlope(tile, NULL); frosch@8909: if (slope != SLOPE_FLAT && (p2 != 2 || !IsInclinedSlope(slope))) { tron@3636: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); tron@3636: } truelight@0: belugas@6928: /* can't make water of water! */ peter1138@8856: if (IsTileType(tile, MP_WATER) && (!IsTileOwner(tile, OWNER_WATER) || p2 == 1)) continue; Darkvater@1632: celestar@5573: ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); celestar@5573: if (CmdFailed(ret)) return ret; rubidium@7446: cost.AddCost(ret); tron@3183: tron@3189: if (flags & DC_EXEC) { peter1138@8856: if (TileHeight(tile) == 0 && p2 == 1) { celestar@5573: MakeWater(tile); peter1138@8856: } else if (p2 == 2) { peter1138@8864: MakeRiver(tile, Random()); celestar@5573: } else { peter1138@8864: MakeCanal(tile, _current_player, Random()); celestar@5573: } tron@3189: MarkTileDirtyByTile(tile); frosch@8939: MarkCanalsAndRiversAroundDirty(tile); tron@3189: } tron@3183: rubidium@7446: cost.AddCost(_price.clear_water); Darkvater@1632: } END_TILE_LOOP(tile, size_x, size_y, 0); truelight@0: rubidium@7446: if (cost.GetCost() == 0) { tron@3183: return_cmd_error(STR_1007_ALREADY_BUILT); tron@3183: } else { tron@3183: return cost; tron@3183: } truelight@0: } truelight@0: rubidium@7439: static CommandCost ClearTile_Water(TileIndex tile, byte flags) tron@1977: { celestar@3425: switch (GetWaterTileType(tile)) { rubidium@6507: case WATER_TILE_CLEAR: celestar@3425: if (flags & DC_NO_WATER) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); truelight@0: belugas@6928: /* Make sure it's not an edge tile. */ skidd13@8450: if (!IsInsideMM(TileX(tile), 1, MapMaxX() - 1) || skidd13@8450: !IsInsideMM(TileY(tile), 1, MapMaxY() - 1)) { celestar@3425: return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP); celestar@3425: } tron@3017: rubidium@8005: /* Make sure no vehicle is on the tile */ rubidium@8254: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; rubidium@8005: rubidium@7766: if (GetTileOwner(tile) != OWNER_WATER && GetTileOwner(tile) != OWNER_NONE && !CheckTileOwnership(tile)) return CMD_ERROR; peter1138@3940: frosch@8939: if (flags & DC_EXEC) { frosch@8939: DoClearSquare(tile); frosch@8939: MarkCanalsAndRiversAroundDirty(tile); frosch@8939: } rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_water); truelight@0: rubidium@6507: case WATER_TILE_COAST: { tron@3636: Slope slope = GetTileSlope(tile, NULL); tron@3439: belugas@6928: /* Make sure no vehicle is on the tile */ rubidium@8254: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; tron@3439: frosch@8939: if (flags & DC_EXEC) { frosch@8939: DoClearSquare(tile); frosch@8939: MarkCanalsAndRiversAroundDirty(tile); frosch@8939: } frosch@8909: if (IsSlopeWithOneCornerRaised(slope)) { rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_water); tron@3439: } else { rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_roughland); celestar@3425: } tron@3439: } tron@3439: rubidium@6507: case WATER_TILE_LOCK: { tron@3439: static const TileIndexDiffC _shiplift_tomiddle_offs[] = { tron@3439: { 0, 0}, {0, 0}, { 0, 0}, {0, 0}, // middle tron@3439: {-1, 0}, {0, 1}, { 1, 0}, {0, -1}, // lower tron@3439: { 1, 0}, {0, -1}, {-1, 0}, {0, 1}, // upper tron@3439: }; tron@3439: tron@3439: if (flags & DC_AUTO) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); tron@3439: if (_current_player == OWNER_WATER) return CMD_ERROR; belugas@6928: /* move to the middle tile.. */ tron@3439: return RemoveShiplift(tile + ToTileIndexDiff(_shiplift_tomiddle_offs[GetSection(tile)]), flags); tron@3439: } tron@3439: rubidium@6507: case WATER_TILE_DEPOT: celestar@3425: if (flags & DC_AUTO) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); celestar@3425: return RemoveShipDepot(tile, flags); tron@3439: tron@3439: default: tron@3439: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: frosch@8937: /** frosch@8937: * return true if a tile is a water tile wrt. a certain direction. frosch@8937: * frosch@8937: * @param tile The tile of interest. frosch@8937: * @param from The direction of interest. frosch@8937: * @return true iff the tile is water in the view of 'from'. frosch@8937: * frosch@8937: */ frosch@8937: static bool IsWateredTile(TileIndex tile, Direction from) truelight@0: { tron@1214: switch (GetTileType(tile)) { tron@3977: case MP_WATER: peter1138@4087: if (!IsCoast(tile)) return true; frosch@8937: switch (GetTileSlope(tile, NULL)) { frosch@8937: case SLOPE_W: return (from == DIR_SE) || (from == DIR_E) || (from == DIR_NE); frosch@8937: case SLOPE_S: return (from == DIR_NE) || (from == DIR_N) || (from == DIR_NW); frosch@8937: case SLOPE_E: return (from == DIR_NW) || (from == DIR_W) || (from == DIR_SW); frosch@8937: case SLOPE_N: return (from == DIR_SW) || (from == DIR_S) || (from == DIR_SE); frosch@8937: default: return false; frosch@8937: } tron@3977: frosch@8910: case MP_RAILWAY: frosch@8910: if (GetRailGroundType(tile) == RAIL_GROUND_WATER) { frosch@8910: assert(IsPlainRailTile(tile)); frosch@8937: switch (GetTileSlope(tile, NULL)) { frosch@8937: case SLOPE_W: return (from == DIR_SE) || (from == DIR_E) || (from == DIR_NE); frosch@8937: case SLOPE_S: return (from == DIR_NE) || (from == DIR_N) || (from == DIR_NW); frosch@8937: case SLOPE_E: return (from == DIR_NW) || (from == DIR_W) || (from == DIR_SW); frosch@8937: case SLOPE_N: return (from == DIR_SW) || (from == DIR_S) || (from == DIR_SE); frosch@8937: default: return false; frosch@8937: } frosch@8910: } frosch@8910: return false; frosch@8910: peter1138@8967: case MP_STATION: return IsOilRig(tile) || (IsDock(tile) && GetTileSlope(tile, NULL) == SLOPE_FLAT) || IsBuoy(tile); rubidium@8003: case MP_INDUSTRY: return (GetIndustrySpec(GetIndustryType(tile))->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0; rubidium@8003: default: return false; tron@1048: } truelight@0: } truelight@0: peter1138@8856: static void DrawWaterEdges(SpriteID base, TileIndex tile) truelight@0: { truelight@0: uint wa; truelight@193: belugas@6928: /* determine the edges around with water. */ frosch@8937: wa = IsWateredTile(TILE_ADDXY(tile, -1, 0), DIR_SW) << 0; frosch@8937: wa += IsWateredTile(TILE_ADDXY(tile, 0, 1), DIR_NW) << 1; frosch@8937: wa += IsWateredTile(TILE_ADDXY(tile, 1, 0), DIR_NE) << 2; frosch@8937: wa += IsWateredTile(TILE_ADDXY(tile, 0, -1), DIR_SE) << 3; truelight@193: peter1138@8856: if (!(wa & 1)) DrawGroundSprite(base, PAL_NONE); peter1138@8856: if (!(wa & 2)) DrawGroundSprite(base + 1, PAL_NONE); peter1138@8856: if (!(wa & 4)) DrawGroundSprite(base + 2, PAL_NONE); peter1138@8856: if (!(wa & 8)) DrawGroundSprite(base + 3, PAL_NONE); truelight@0: belugas@6928: /* right corner */ tron@2989: switch (wa & 0x03) { peter1138@8856: case 0: DrawGroundSprite(base + 4, PAL_NONE); break; frosch@8937: case 3: if (!IsWateredTile(TILE_ADDXY(tile, -1, 1), DIR_W)) DrawGroundSprite(base + 8, PAL_NONE); break; tron@2989: } truelight@0: belugas@6928: /* bottom corner */ tron@2989: switch (wa & 0x06) { peter1138@8856: case 0: DrawGroundSprite(base + 5, PAL_NONE); break; frosch@8937: case 6: if (!IsWateredTile(TILE_ADDXY(tile, 1, 1), DIR_N)) DrawGroundSprite(base + 9, PAL_NONE); break; tron@2989: } truelight@0: belugas@6928: /* left corner */ tron@2989: switch (wa & 0x0C) { peter1138@8856: case 0: DrawGroundSprite(base + 6, PAL_NONE); break; frosch@8937: case 12: if (!IsWateredTile(TILE_ADDXY(tile, 1, -1), DIR_E)) DrawGroundSprite(base + 10, PAL_NONE); break; tron@2989: } truelight@0: belugas@6928: /* upper corner */ tron@2989: switch (wa & 0x09) { peter1138@8856: case 0: DrawGroundSprite(base + 7, PAL_NONE); break; frosch@8937: case 9: if (!IsWateredTile(TILE_ADDXY(tile, -1, -1), DIR_S)) DrawGroundSprite(base + 11, PAL_NONE); break; tron@2989: } truelight@0: } truelight@0: peter1138@8967: /** Draw a plain sea water tile with no edges */ frosch@8992: static void DrawSeaWater(TileIndex tile) peter1138@8967: { peter1138@8967: DrawGroundSprite(SPR_FLAT_WATER_TILE, PAL_NONE); peter1138@8967: } peter1138@8967: peter1138@8856: /** draw a canal styled water tile with dikes around */ frosch@8992: static void DrawCanalWater(TileIndex tile) peter1138@8856: { frosch@8992: DrawGroundSprite(SPR_FLAT_WATER_TILE, PAL_NONE); peter1138@8967: peter1138@8856: /* Test for custom graphics, else use the default */ peter1138@8856: SpriteID dikes_base = GetCanalSprite(CF_DIKES, tile); peter1138@8856: if (dikes_base == 0) dikes_base = SPR_CANAL_DIKES_BASE; peter1138@8856: peter1138@8856: DrawWaterEdges(dikes_base, tile); peter1138@8856: } peter1138@8856: rubidium@6574: struct LocksDrawTileStruct { truelight@0: int8 delta_x, delta_y, delta_z; truelight@0: byte width, height, depth; truelight@0: SpriteID image; rubidium@6574: }; truelight@0: truelight@0: #include "table/water_land.h" truelight@0: Darkvater@2436: static void DrawWaterStuff(const TileInfo *ti, const WaterDrawTileStruct *wdts, frosch@8992: SpriteID palette, uint base, bool draw_ground tron@1399: ) truelight@0: { peter1138@7079: SpriteID image; peter1138@7079: SpriteID water_base = GetCanalSprite(CF_WATERSLOPE, ti->tile); peter1138@7079: SpriteID locks_base = GetCanalSprite(CF_LOCKS, ti->tile); peter1138@7079: peter1138@7079: /* If no custom graphics, use defaults */ rubidium@8378: if (water_base == 0) water_base = SPR_CANALS_BASE; peter1138@7079: if (locks_base == 0) { rubidium@8378: locks_base = SPR_SHIPLIFT_BASE; peter1138@7079: } else { peter1138@7079: /* If using custom graphics, ignore the variation on height */ peter1138@7079: base = 0; peter1138@7079: } peter1138@7079: peter1138@7079: image = wdts++->image; peter1138@7079: if (image < 4) image += water_base; frosch@8992: if (draw_ground) DrawGroundSprite(image, PAL_NONE); truelight@193: tron@1399: for (; wdts->delta_x != 0x80; wdts++) { rubidium@7829: AddSortableSpriteToDraw(wdts->image + base + ((wdts->image < 24) ? locks_base : 0), palette, peter1138@5919: ti->x + wdts->delta_x, ti->y + wdts->delta_y, peter1138@5919: wdts->width, wdts->height, rubidium@7829: wdts->unk, ti->z + wdts->delta_z, belugas@8345: IsTransparencySet(TO_BUILDINGS)); truelight@0: } truelight@0: } truelight@0: frosch@8992: static void DrawRiverWater(const TileInfo *ti) peter1138@8856: { peter1138@8856: SpriteID image = SPR_FLAT_WATER_TILE; peter1138@8856: SpriteID edges_base = GetCanalSprite(CF_RIVER_EDGE, ti->tile); peter1138@8856: peter1138@8856: if (ti->tileh != SLOPE_FLAT) { peter1138@8856: image = GetCanalSprite(CF_RIVER_SLOPE, ti->tile); peter1138@8856: if (image == 0) { frosch@8926: switch (ti->tileh) { frosch@8926: case SLOPE_NW: image = SPR_WATER_SLOPE_Y_DOWN; break; frosch@8926: case SLOPE_SW: image = SPR_WATER_SLOPE_X_UP; break; frosch@8926: case SLOPE_SE: image = SPR_WATER_SLOPE_Y_UP; break; frosch@8926: case SLOPE_NE: image = SPR_WATER_SLOPE_X_DOWN; break; frosch@8926: default: image = SPR_FLAT_WATER_TILE; break; frosch@8926: } peter1138@8856: } else { peter1138@8856: switch (ti->tileh) { peter1138@8856: default: NOT_REACHED(); peter1138@8856: case SLOPE_SE: edges_base += 12; break; peter1138@8856: case SLOPE_NE: image += 1; edges_base += 24; break; peter1138@8856: case SLOPE_SW: image += 2; edges_base += 36; break; peter1138@8856: case SLOPE_NW: image += 3; edges_base += 48; break; peter1138@8856: } peter1138@8856: } peter1138@8856: } peter1138@8856: frosch@8992: DrawGroundSprite(image, PAL_NONE); peter1138@8856: frosch@8926: /* Draw river edges if available. */ frosch@8926: if (edges_base > 48) DrawWaterEdges(edges_base, ti->tile); peter1138@8856: } peter1138@8856: frosch@8876: void DrawShoreTile(Slope tileh) frosch@8876: { frosch@8876: /* Converts the enum Slope into an offset based on SPR_SHORE_BASE. frosch@8876: * This allows to calculate the proper sprite to display for this Slope */ frosch@8876: static const byte tileh_to_shoresprite[32] = { frosch@8876: 0, 1, 2, 3, 4, 16, 6, 7, 8, 9, 17, 11, 12, 13, 14, 0, frosch@8876: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 10, 15, 0, frosch@8876: }; frosch@8876: frosch@8876: assert(!IsHalftileSlope(tileh)); // Halftile slopes need to get handled earlier. frosch@8876: assert(tileh != SLOPE_FLAT); // Shore is never flat frosch@8876: frosch@8876: assert((tileh != SLOPE_EW) && (tileh != SLOPE_NS)); // No suitable sprites for current flooding behaviour frosch@8876: frosch@8876: DrawGroundSprite(SPR_SHORE_BASE + tileh_to_shoresprite[tileh], PAL_NONE); frosch@8876: } frosch@8876: frosch@8992: void DrawWaterClassGround(const TileInfo *ti) { frosch@8992: switch (GetWaterClass(ti->tile)) { frosch@8992: case WATER_CLASS_SEA: DrawSeaWater(ti->tile); break; frosch@8992: case WATER_CLASS_CANAL: DrawCanalWater(ti->tile); break; frosch@8992: case WATER_CLASS_RIVER: DrawRiverWater(ti); break; frosch@8992: } frosch@8992: } frosch@8992: truelight@0: static void DrawTile_Water(TileInfo *ti) truelight@0: { celestar@3402: switch (GetWaterTileType(ti->tile)) { rubidium@6507: case WATER_TILE_CLEAR: frosch@8992: DrawWaterClassGround(ti); celestar@5573: DrawBridgeMiddle(ti); celestar@3402: break; truelight@0: belugas@8660: case WATER_TILE_COAST: { frosch@8876: DrawShoreTile(ti->tileh); celestar@5573: DrawBridgeMiddle(ti); belugas@8660: } break; truelight@0: rubidium@6507: case WATER_TILE_LOCK: { celestar@3425: const WaterDrawTileStruct *t = _shiplift_display_seq[GetSection(ti->tile)]; frosch@8992: DrawWaterStuff(ti, t, 0, ti->z > t[3].delta_y ? 24 : 0, true); celestar@3402: } break; celestar@3402: rubidium@6507: case WATER_TILE_DEPOT: frosch@8992: DrawWaterClassGround(ti); frosch@8992: DrawWaterStuff(ti, _shipdepot_display_seq[GetSection(ti->tile)], PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)), 0, false); celestar@3402: break; truelight@0: } truelight@0: } truelight@0: truelight@0: void DrawShipDepotSprite(int x, int y, int image) truelight@0: { tron@1399: const WaterDrawTileStruct *wdts = _shipdepot_display_seq[image]; truelight@0: peter1138@5919: DrawSprite(wdts++->image, PAL_NONE, x, y); truelight@193: tron@1399: for (; wdts->delta_x != 0x80; wdts++) { truelight@0: Point pt = RemapCoords(wdts->delta_x, wdts->delta_y, wdts->delta_z); peter1138@5919: DrawSprite(wdts->image, PLAYER_SPRITE_COLOR(_local_player), x + pt.x, y + pt.y); truelight@0: } truelight@0: } truelight@0: truelight@0: tron@4231: static uint GetSlopeZ_Water(TileIndex tile, uint x, uint y) truelight@193: { tron@4231: uint z; rubidium@5838: Slope tileh = GetTileSlope(tile, &z); tron@4231: tron@4231: return z + GetPartialZ(x & 0xF, y & 0xF, tileh); truelight@0: } truelight@0: rubidium@7831: static Foundation GetFoundation_Water(TileIndex tile, Slope tileh) truelight@193: { rubidium@7831: return FOUNDATION_NONE; dominik@39: } dominik@39: tron@1977: static void GetAcceptedCargo_Water(TileIndex tile, AcceptedCargo ac) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: tron@1977: static void GetTileDesc_Water(TileIndex tile, TileDesc *td) truelight@0: { celestar@3402: switch (GetWaterTileType(tile)) { rubidium@6507: case WATER_TILE_CLEAR: rubidium@8235: if (!IsCanal(tile)) { celestar@3402: td->str = STR_3804_WATER; celestar@3402: } else { celestar@3402: td->str = STR_LANDINFO_CANAL; celestar@3402: } celestar@3402: break; rubidium@6507: case WATER_TILE_COAST: td->str = STR_3805_COAST_OR_RIVERBANK; break; rubidium@6507: case WATER_TILE_LOCK : td->str = STR_LANDINFO_LOCK; break; rubidium@6507: case WATER_TILE_DEPOT: td->str = STR_3806_SHIP_DEPOT; break; celestar@3402: default: assert(0); break; tron@2951: } truelight@0: tron@1901: td->owner = GetTileOwner(tile); truelight@0: } truelight@0: tron@1977: static void AnimateTile_Water(TileIndex tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: rubidium@6191: /** rubidium@6191: * Finds a vehicle to flood. rubidium@6191: * It does not find vehicles that are already crashed on bridges, i.e. flooded. rubidium@6191: * @param tile the tile where to find a vehicle to flood rubidium@6191: * @return a vehicle too flood or NULL when there is no vehicle too flood. rubidium@6191: */ rubidium@6191: static Vehicle *FindFloodableVehicleOnTile(TileIndex tile) rubidium@6191: { rubidium@7402: if (IsTileType(tile, MP_STATION) && IsAirport(tile)) { rubidium@7402: const Station *st = GetStationByTile(tile); rubidium@7402: const AirportFTAClass *airport = st->Airport(); rubidium@7402: for (uint x = 0; x < airport->size_x; x++) { rubidium@7402: for (uint y = 0; y < airport->size_y; y++) { rubidium@7402: tile = TILE_ADDXY(st->airport_tile, x, y); rubidium@7402: Vehicle *v = FindVehicleOnTileZ(tile, 1 + airport->delta_z); rubidium@7402: if (v != NULL && (v->vehstatus & VS_CRASHED) == 0) return v; rubidium@7402: } rubidium@7402: } rubidium@7402: rubidium@7402: /* No vehicle could be flooded on this airport anymore */ rubidium@7402: return NULL; rubidium@7402: } rubidium@7402: smatz@8510: /* if non-uniform stations are disabled, flood some train in this train station (if there is any) */ smatz@8510: if (!_patches.nonuniform_stations && IsTileType(tile, MP_STATION) && GetStationType(tile) == STATION_RAIL) { smatz@8510: const Station *st = GetStationByTile(tile); smatz@8510: smatz@8510: BEGIN_TILE_LOOP(t, st->trainst_w, st->trainst_h, st->train_tile) smatz@8510: if (st->TileBelongsToRailStation(t)) { smatz@8510: Vehicle *v = FindVehicleOnTileZ(t, 0); smatz@8510: if (v != NULL && (v->vehstatus & VS_CRASHED) == 0) return v; smatz@8510: } smatz@8510: END_TILE_LOOP(t, st->trainst_w, st->trainst_h, st->train_tile) smatz@8510: smatz@8510: return NULL; smatz@8510: } smatz@8510: rubidium@6191: if (!IsBridgeTile(tile)) return FindVehicleOnTileZ(tile, 0); rubidium@6191: rubidium@6191: TileIndex end = GetOtherBridgeEnd(tile); rubidium@6191: byte z = GetBridgeHeight(tile); rubidium@6191: Vehicle *v; rubidium@6191: rubidium@6191: /* check the start tile first since as this is closest to the water */ rubidium@6191: v = FindVehicleOnTileZ(tile, z); rubidium@6191: if (v != NULL && (v->vehstatus & VS_CRASHED) == 0) return v; rubidium@6191: rubidium@6191: /* check a vehicle in between both bridge heads */ rubidium@6191: v = FindVehicleBetween(tile, end, z, true); rubidium@6191: if (v != NULL) return v; rubidium@6191: rubidium@6191: /* check the end tile last to give fleeing vehicles a chance to escape */ rubidium@6191: v = FindVehicleOnTileZ(end, z); rubidium@6191: return (v != NULL && (v->vehstatus & VS_CRASHED) == 0) ? v : NULL; rubidium@6191: } rubidium@6191: darkvater@149: static void FloodVehicle(Vehicle *v) darkvater@149: { darkvater@149: if (!(v->vehstatus & VS_CRASHED)) { darkvater@168: uint16 pass = 0; darkvater@149: rubidium@7402: if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_AIRCRAFT) { rubidium@7402: if (v->type == VEH_AIRCRAFT) { rubidium@7402: /* Crashing aircraft are always at z_pos == 1, never on z_pos == 0, rubidium@7402: * because that's always the shadow. Except for the heliport, because rubidium@7402: * that station has a big z_offset for the aircraft. */ rubidium@7402: if (!IsTileType(v->tile, MP_STATION) || !IsAirport(v->tile) || GetTileMaxZ(v->tile) != 0) return; rubidium@7402: const Station *st = GetStationByTile(v->tile); rubidium@7402: const AirportFTAClass *airport = st->Airport(); rubidium@7402: rubidium@7402: if (v->z_pos != airport->delta_z + 1) return; rubidium@7402: } belugas@4171: Vehicle *u; truelight@193: rubidium@7993: if (v->type != VEH_AIRCRAFT) v = v->First(); darkvater@149: u = v; darkvater@149: maedhros@7353: /* crash all wagons, and count passengers */ darkvater@149: BEGIN_ENUM_WAGONS(v) rubidium@7506: if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) pass += v->cargo.Count(); darkvater@149: v->vehstatus |= VS_CRASHED; smatz@8813: MarkSingleVehicleDirty(v); darkvater@149: END_ENUM_WAGONS(v) darkvater@149: darkvater@149: v = u; maedhros@7353: rubidium@7402: switch (v->type) { rubidium@7402: default: NOT_REACHED(); rubidium@7402: case VEH_TRAIN: rubidium@7402: if (IsFrontEngine(v)) pass += 4; // driver rubidium@7402: v->u.rail.crash_anim_pos = 4000; // max 4440, disappear pretty fast rubidium@7402: break; rubidium@7402: rubidium@7402: case VEH_ROAD: rubidium@7402: if (IsRoadVehFront(v)) pass += 1; // driver rubidium@7402: v->u.road.crashed_ctr = 2000; // max 2220, disappear pretty fast rubidium@7402: break; rubidium@7402: rubidium@7402: case VEH_AIRCRAFT: rubidium@7402: pass += 2; // driver rubidium@7402: v->u.air.crashed_counter = 9000; // max 10000, disappear pretty fast rubidium@7402: break; maedhros@7353: } maedhros@7353: tron@588: RebuildVehicleLists(); tron@2549: } else { darkvater@168: return; tron@2549: } darkvater@149: smatz@8846: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); darkvater@149: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@193: tron@534: SetDParam(0, pass); darkvater@149: AddNewsItem(STR_B006_FLOOD_VEHICLE_DESTROYED, rubidium@6987: NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ACCIDENT, 0), darkvater@149: v->index, darkvater@149: 0); tron@2549: CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE); tron@2549: SndPlayVehicleFx(SND_12_EXPLOSION, v); dominik@714: } darkvater@149: } darkvater@149: belugas@8227: /** frosch@8876: * Returns the behaviour of a tile during flooding. frosch@8876: * frosch@8876: * @return Behaviour of the tile frosch@8876: */ frosch@8876: static FloodingBehaviour GetFloodingBehaviour(TileIndex tile) frosch@8876: { frosch@8993: /* FLOOD_ACTIVE: 'single-corner-raised'-coast, sea, sea-shipdepots, sea-buoys, sea-docks (water part), rail with flooded halftile frosch@8955: * FLOOD_DRYUP: coast with more than one corner raised, coast with rail-track, coast with trees frosch@8993: * FLOOD_PASSIVE: oilrig, water-industries frosch@8876: * FLOOD_NONE: canals, rivers, everything else frosch@8876: */ frosch@8876: switch (GetTileType(tile)) { frosch@8876: case MP_WATER: frosch@8876: if (IsCoast(tile)) { frosch@8876: Slope tileh = GetTileSlope(tile, NULL); frosch@8909: return (IsSlopeWithOneCornerRaised(tileh) ? FLOOD_ACTIVE : FLOOD_DRYUP); frosch@8876: } else { peter1138@8967: return (GetWaterClass(tile) == WATER_CLASS_SEA) ? FLOOD_ACTIVE : FLOOD_NONE; frosch@8876: } frosch@8876: frosch@8876: case MP_RAILWAY: frosch@8910: if (GetRailGroundType(tile) == RAIL_GROUND_WATER) { frosch@8910: return (IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL)) ? FLOOD_ACTIVE : FLOOD_DRYUP); frosch@8910: } frosch@8910: return FLOOD_NONE; frosch@8876: frosch@8955: case MP_TREES: frosch@8955: return (GetTreeGround(tile) == TREE_GROUND_SHORE ? FLOOD_DRYUP : FLOOD_NONE); frosch@8955: frosch@8876: case MP_STATION: frosch@8993: if (IsBuoy(tile) || (IsDock(tile) && GetTileSlope(tile, NULL) == SLOPE_FLAT)) { frosch@8993: return (GetWaterClass(tile) == WATER_CLASS_SEA ? FLOOD_ACTIVE : FLOOD_NONE); frosch@8993: } frosch@8993: return (IsOilRig(tile) ? FLOOD_PASSIVE : FLOOD_NONE); frosch@8876: frosch@8876: case MP_INDUSTRY: frosch@8876: return ((GetIndustrySpec(GetIndustryType(tile))->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0 ? FLOOD_PASSIVE : FLOOD_NONE); frosch@8876: frosch@8876: default: frosch@8876: return FLOOD_NONE; frosch@8876: } frosch@8876: } frosch@8876: frosch@8876: /** frosch@8876: * Floods a tile. frosch@8876: */ frosch@8876: static void DoFloodTile(TileIndex target) frosch@8876: { frosch@8876: if (IsTileType(target, MP_WATER)) return; frosch@8876: frosch@8876: bool flooded = false; // Will be set to true if something is changed. frosch@8876: frosch@8876: _current_player = OWNER_WATER; frosch@8876: frosch@8955: Slope tileh = GetTileSlope(target, NULL); frosch@8955: if (tileh != SLOPE_FLAT) { frosch@8876: /* make coast.. */ frosch@8876: switch (GetTileType(target)) { frosch@8876: case MP_RAILWAY: { frosch@8876: if (!IsPlainRailTile(target)) break; frosch@8876: frosch@8876: flooded = FloodHalftile(target); frosch@8876: frosch@8876: Vehicle *v = FindFloodableVehicleOnTile(target); frosch@8876: if (v != NULL) FloodVehicle(v); frosch@8876: frosch@8876: break; frosch@8876: } frosch@8876: frosch@8955: case MP_TREES: frosch@8955: if (!IsSlopeWithOneCornerRaised(tileh)) { frosch@8955: SetTreeGroundDensity(target, TREE_GROUND_SHORE, 3); frosch@8955: MarkTileDirtyByTile(target); frosch@8955: flooded = true; frosch@8955: break; frosch@8955: } frosch@8955: /* FALL THROUGH */ frosch@8876: case MP_CLEAR: frosch@8876: if (CmdSucceeded(DoCommand(target, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) { frosch@8876: MakeShore(target); frosch@8876: MarkTileDirtyByTile(target); frosch@8876: flooded = true; frosch@8876: } frosch@8876: break; frosch@8876: frosch@8876: default: frosch@8876: break; frosch@8876: } frosch@8876: } else { frosch@8876: /* Flood vehicles */ frosch@8876: Vehicle *v = FindFloodableVehicleOnTile(target); frosch@8876: if (v != NULL) FloodVehicle(v); frosch@8876: frosch@8876: /* flood flat tile */ frosch@8876: if (CmdSucceeded(DoCommand(target, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) { frosch@8876: MakeWater(target); frosch@8876: MarkTileDirtyByTile(target); frosch@8876: flooded = true; frosch@8876: } frosch@8876: } frosch@8876: frosch@8876: if (flooded) { frosch@8876: /* Mark surrounding canal tiles dirty too to avoid glitches */ frosch@8939: MarkCanalsAndRiversAroundDirty(target); frosch@8876: frosch@8876: /* update signals if needed */ frosch@8876: UpdateSignalsInBuffer(); frosch@8876: } frosch@8876: frosch@8876: _current_player = OWNER_NONE; frosch@8876: } frosch@8876: frosch@8876: /** frosch@8876: * Drys a tile up. frosch@8876: */ frosch@8876: static void DoDryUp(TileIndex tile) frosch@8876: { frosch@8876: _current_player = OWNER_WATER; frosch@8876: frosch@8910: switch (GetTileType(tile)) { frosch@8910: case MP_RAILWAY: frosch@8910: assert(IsPlainRailTile(tile)); frosch@8910: assert(GetRailGroundType(tile) == RAIL_GROUND_WATER); frosch@8910: frosch@8910: RailGroundType new_ground; frosch@8910: switch (GetTrackBits(tile)) { frosch@8910: case TRACK_BIT_UPPER: new_ground = RAIL_GROUND_FENCE_HORIZ1; break; frosch@8910: case TRACK_BIT_LOWER: new_ground = RAIL_GROUND_FENCE_HORIZ2; break; frosch@8910: case TRACK_BIT_LEFT: new_ground = RAIL_GROUND_FENCE_VERT1; break; frosch@8910: case TRACK_BIT_RIGHT: new_ground = RAIL_GROUND_FENCE_VERT2; break; frosch@8910: default: NOT_REACHED(); frosch@8910: } frosch@8910: SetRailGroundType(tile, new_ground); frosch@8910: MarkTileDirtyByTile(tile); frosch@8910: break; frosch@8910: frosch@8955: case MP_TREES: frosch@8955: SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3); frosch@8955: MarkTileDirtyByTile(tile); frosch@8955: break; frosch@8955: frosch@8910: case MP_WATER: frosch@8910: assert(IsCoast(tile)); frosch@8910: frosch@8910: if (CmdSucceeded(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) { frosch@8910: MakeClear(tile, CLEAR_GRASS, 3); frosch@8910: MarkTileDirtyByTile(tile); frosch@8910: } frosch@8910: break; frosch@8910: frosch@8910: default: NOT_REACHED(); frosch@8876: } frosch@8876: frosch@8876: _current_player = OWNER_NONE; frosch@8876: } frosch@8876: frosch@8876: /** belugas@8227: * Let a water tile floods its diagonal adjoining tiles rubidium@8267: * called from tunnelbridge_cmd, and by TileLoop_Industry() and TileLoop_Track() belugas@8227: * belugas@8227: * @param tile the water/shore tile that floods belugas@8227: */ tron@1977: void TileLoop_Water(TileIndex tile) truelight@0: { frosch@8876: switch (GetFloodingBehaviour(tile)) { frosch@8876: case FLOOD_ACTIVE: frosch@8876: for (Direction dir = DIR_BEGIN; dir < DIR_END; dir++) { frosch@8876: TileIndex dest = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDir(dir)); frosch@8876: if (dest == INVALID_TILE) continue; tron@2989: frosch@8876: uint z_dest; frosch@8876: Slope slope_dest = (Slope)(GetFoundationSlope(dest, &z_dest) & ~SLOPE_HALFTILE_MASK & ~SLOPE_STEEP); frosch@8876: if (z_dest > 0) continue; dominik@43: frosch@8876: if (!HasBit(_flood_from_dirs[slope_dest], ReverseDir(dir))) continue; dominik@43: frosch@8876: DoFloodTile(dest); frosch@8876: } frosch@8876: break; dominik@43: frosch@8876: case FLOOD_DRYUP: { frosch@8876: Slope slope_here = (Slope)(GetFoundationSlope(tile, NULL) & ~SLOPE_HALFTILE_MASK & ~SLOPE_STEEP); frosch@8876: uint check_dirs = _flood_from_dirs[slope_here]; frosch@8876: uint dir; frosch@8876: FOR_EACH_SET_BIT(dir, check_dirs) { frosch@8876: TileIndex dest = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDir((Direction)dir)); frosch@8876: if (dest == INVALID_TILE) continue; frosch@8876: frosch@8876: FloodingBehaviour dest_behaviour = GetFloodingBehaviour(dest); frosch@8876: if ((dest_behaviour == FLOOD_ACTIVE) || (dest_behaviour == FLOOD_PASSIVE)) return; frosch@8876: } frosch@8876: DoDryUp(tile); frosch@8876: break; frosch@8876: } frosch@8876: frosch@8876: default: return; tron@2639: } frosch@8876: } dominik@43: frosch@8876: void ConvertGroundTilesIntoWaterTiles() frosch@8876: { frosch@8876: TileIndex tile; frosch@8876: uint z; frosch@8876: Slope slope; frosch@8876: frosch@8876: for (tile = 0; tile < MapSize(); ++tile) { frosch@8876: slope = GetTileSlope(tile, &z); frosch@8876: if (IsTileType(tile, MP_CLEAR) && z == 0) { frosch@8876: /* Make both water for tiles at level 0 frosch@8876: * and make shore, as that looks much better frosch@8876: * during the generation. */ frosch@8876: switch (slope) { frosch@8876: case SLOPE_FLAT: frosch@8876: MakeWater(tile); frosch@8876: break; frosch@8876: frosch@8876: case SLOPE_N: frosch@8876: case SLOPE_E: frosch@8876: case SLOPE_S: frosch@8876: case SLOPE_W: frosch@8876: MakeShore(tile); frosch@8876: break; frosch@8876: frosch@8876: default: frosch@8876: uint check_dirs = _flood_from_dirs[slope & ~SLOPE_STEEP]; frosch@8876: uint dir; frosch@8876: FOR_EACH_SET_BIT(dir, check_dirs) { frosch@8876: TileIndex dest = TILE_ADD(tile, TileOffsByDir((Direction)dir)); frosch@8876: Slope slope_dest = (Slope)(GetTileSlope(dest, NULL) & ~SLOPE_STEEP); frosch@8909: if (slope_dest == SLOPE_FLAT || IsSlopeWithOneCornerRaised(slope_dest)) { frosch@8876: MakeShore(tile); frosch@8876: break; frosch@8876: } frosch@8876: } frosch@8876: break; frosch@8876: } frosch@8876: } tron@2639: } truelight@0: } truelight@0: rubidium@7179: static uint32 GetTileTrackStatus_Water(TileIndex tile, TransportType mode, uint sub_mode) truelight@0: { celestar@3423: static const byte coast_tracks[] = {0, 32, 4, 0, 16, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0}; tron@4169: tron@4169: TrackBits ts; tron@4169: tron@2951: if (mode != TRANSPORT_WATER) return 0; truelight@0: celestar@3423: switch (GetWaterTileType(tile)) { peter1138@8967: case WATER_TILE_CLEAR: ts = (GetTileSlope(tile, NULL) == SLOPE_FLAT) ? TRACK_BIT_ALL : TRACK_BIT_NONE; break; rubidium@6507: case WATER_TILE_COAST: ts = (TrackBits)coast_tracks[GetTileSlope(tile, NULL) & 0xF]; break; rubidium@6507: case WATER_TILE_LOCK: ts = AxisToTrackBits(DiagDirToAxis(GetLockDirection(tile))); break; rubidium@6507: case WATER_TILE_DEPOT: ts = AxisToTrackBits(GetShipDepotAxis(tile)); break; celestar@3423: default: return 0; truelight@0: } KUDr@3900: if (TileX(tile) == 0) { belugas@6928: /* NE border: remove tracks that connects NE tile edge */ KUDr@3900: ts &= ~(TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT); KUDr@3900: } KUDr@3900: if (TileY(tile) == 0) { belugas@6928: /* NW border: remove tracks that connects NW tile edge */ KUDr@3900: ts &= ~(TRACK_BIT_Y | TRACK_BIT_LEFT | TRACK_BIT_UPPER); KUDr@3900: } KUDr@3900: return ts * 0x101; truelight@0: } truelight@0: tron@1977: static void ClickTile_Water(TileIndex tile) truelight@0: { rubidium@6507: if (GetWaterTileType(tile) == WATER_TILE_DEPOT) { celestar@3423: TileIndex tile2 = GetOtherShipDepotTile(tile); truelight@193: rubidium@6585: ShowDepotWindow(tile < tile2 ? tile : tile2, VEH_SHIP); truelight@0: } truelight@0: } truelight@0: Darkvater@2436: static void ChangeTileOwner_Water(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { tron@1901: if (!IsTileOwner(tile, old_player)) return; truelight@0: Darkvater@4848: if (new_player != PLAYER_SPECTATOR) { tron@1902: SetTileOwner(tile, new_player); smatz@9016: return; truelight@0: } smatz@9016: smatz@9016: /* Remove depot */ smatz@9016: if (IsShipDepot(tile)) DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR); smatz@9016: smatz@9016: /* Set owner of canals and locks ... and also canal under dock there was before. smatz@9016: * Check if the new owner after removing depot isn't OWNER_WATER. */ smatz@9016: if (IsTileOwner(tile, old_player)) SetTileOwner(tile, OWNER_NONE); truelight@0: } truelight@0: rubidium@8615: static VehicleEnterTileStatus VehicleEnter_Water(Vehicle *v, TileIndex tile, int x, int y) truelight@0: { rubidium@6317: return VETSB_CONTINUE; truelight@0: } truelight@0: rubidium@7990: static CommandCost TerraformTile_Water(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new) rubidium@7990: { rubidium@7990: /* Canals can't be terraformed */ rubidium@8235: if (IsWaterTile(tile) && IsCanal(tile)) return_cmd_error(STR_MUST_DEMOLISH_CANAL_FIRST); rubidium@7990: rubidium@7990: return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@7990: } rubidium@7990: truelight@0: rubidium@5838: extern const TileTypeProcs _tile_type_water_procs = { rubidium@4344: DrawTile_Water, /* draw_tile_proc */ rubidium@4344: GetSlopeZ_Water, /* get_slope_z_proc */ rubidium@4344: ClearTile_Water, /* clear_tile_proc */ rubidium@4344: GetAcceptedCargo_Water, /* get_accepted_cargo_proc */ rubidium@4344: GetTileDesc_Water, /* get_tile_desc_proc */ rubidium@4344: GetTileTrackStatus_Water, /* get_tile_track_status_proc */ rubidium@4344: ClickTile_Water, /* click_tile_proc */ rubidium@4344: AnimateTile_Water, /* animate_tile_proc */ rubidium@4344: TileLoop_Water, /* tile_loop_clear */ rubidium@4344: ChangeTileOwner_Water, /* change_tile_owner_clear */ rubidium@4344: NULL, /* get_produced_cargo_proc */ rubidium@4344: VehicleEnter_Water, /* vehicle_enter_tile_proc */ rubidium@7831: GetFoundation_Water, /* get_foundation_proc */ rubidium@7990: TerraformTile_Water, /* terraform_tile_proc */ truelight@0: };