tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file water_cmd.cpp Handling of water tiles. */ belugas@6432: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@3189: #include "bridge_map.h" rubidium@6160: #include "bridge.h" tron@6134: #include "cmd_helper.h" tron@3338: #include "station_map.h" rubidium@8119: #include "tile_cmd.h" maedhros@6453: #include "landscape.h" rubidium@8224: #include "viewport_func.h" rubidium@8116: #include "command_func.h" truelight@0: #include "town.h" rubidium@8763: #include "news_func.h" rubidium@8962: #include "depot_base.h" rubidium@8962: #include "depot_func.h" matthijs@1752: #include "vehicle_gui.h" bjarni@2676: #include "train.h" maedhros@6857: #include "roadveh.h" rubidium@8108: #include "water.h" tron@3111: #include "water_map.h" rubidium@7507: #include "industry_map.h" peter1138@5210: #include "newgrf.h" peter1138@6583: #include "newgrf_canal.h" belugas@7849: #include "transparency.h" rubidium@8114: #include "strings_func.h" rubidium@8131: #include "functions.h" rubidium@8131: #include "window_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8157: #include "sound_func.h" rubidium@8211: #include "variables.h" rubidium@10208: #include "company_func.h" rubidium@8270: #include "settings_type.h" frosch@8380: #include "clear_map.h" frosch@8459: #include "tree_map.h" rubidium@8785: #include "station_base.h" rubidium@8785: #include "airport.h" rubidium@10083: #include "aircraft.h" rubidium@8787: #include "newgrf_cargo.h" rubidium@9009: #include "effectvehicle_func.h" rubidium@9038: #include "oldpool_func.h" rubidium@9490: #include "tunnelbridge_map.h" darkvater@149: rubidium@8264: #include "table/sprites.h" rubidium@8264: #include "table/strings.h" ludde@2261: frosch@8380: /** frosch@8380: * Describes the behaviour of a tile during flooding. frosch@8380: */ frosch@8380: enum FloodingBehaviour { frosch@8380: FLOOD_NONE, ///< The tile does not flood neighboured tiles. frosch@8380: FLOOD_ACTIVE, ///< The tile floods neighboured tiles. frosch@8380: FLOOD_PASSIVE, ///< The tile does not actively flood neighboured tiles, but it prevents them from drying up. frosch@8380: FLOOD_DRYUP, ///< The tile drys up if it is not constantly flooded from neighboured tiles. frosch@8380: }; frosch@8380: frosch@8380: /** frosch@8380: * Describes from which directions a specific slope can be flooded (if the tile is floodable at all). frosch@8380: */ frosch@8380: static const uint8 _flood_from_dirs[] = { frosch@8380: (1 << DIR_NW) | (1 << DIR_SW) | (1 << DIR_SE) | (1 << DIR_NE), // SLOPE_FLAT frosch@8380: (1 << DIR_NE) | (1 << DIR_SE), // SLOPE_W frosch@8380: (1 << DIR_NW) | (1 << DIR_NE), // SLOPE_S frosch@8380: (1 << DIR_NE), // SLOPE_SW frosch@8380: (1 << DIR_NW) | (1 << DIR_SW), // SLOPE_E frosch@8380: 0, // SLOPE_EW frosch@8380: (1 << DIR_NW), // SLOPE_SE frosch@8380: (1 << DIR_N ) | (1 << DIR_NW) | (1 << DIR_NE), // SLOPE_WSE, SLOPE_STEEP_S frosch@8380: (1 << DIR_SW) | (1 << DIR_SE), // SLOPE_N frosch@8380: (1 << DIR_SE), // SLOPE_NW frosch@8380: 0, // SLOPE_NS frosch@8380: (1 << DIR_E ) | (1 << DIR_NE) | (1 << DIR_SE), // SLOPE_NWS, SLOPE_STEEP_W frosch@8380: (1 << DIR_SW), // SLOPE_NE frosch@8380: (1 << DIR_S ) | (1 << DIR_SW) | (1 << DIR_SE), // SLOPE_ENW, SLOPE_STEEP_N frosch@8380: (1 << DIR_W ) | (1 << DIR_SW) | (1 << DIR_NW), // SLOPE_SEN, SLOPE_STEEP_E frosch@8380: }; frosch@8380: frosch@8380: /** frosch@8443: * Marks tile dirty if it is a canal or river tile. frosch@8443: * Called to avoid glitches when flooding tiles next to canal tile. frosch@8443: * frosch@8443: * @param tile tile to check frosch@8443: */ frosch@8443: static inline void MarkTileDirtyIfCanalOrRiver(TileIndex tile) frosch@8443: { frosch@8443: if (IsTileType(tile, MP_WATER) && (IsCanal(tile) || IsRiver(tile))) MarkTileDirtyByTile(tile); frosch@8443: } frosch@8443: frosch@8443: /** frosch@8443: * Marks the tiles around a tile as dirty, if they are canals or rivers. frosch@8443: * frosch@8443: * @param tile The center of the tile where all other tiles are marked as dirty frosch@8443: * @ingroup dirty frosch@8443: */ frosch@8443: static void MarkCanalsAndRiversAroundDirty(TileIndex tile) frosch@8443: { frosch@8443: for (Direction dir = DIR_BEGIN; dir < DIR_END; dir++) { frosch@8443: MarkTileDirtyIfCanalOrRiver(tile + TileOffsByDir(dir)); frosch@8443: } frosch@8443: } frosch@8443: frosch@8443: /** rubidium@7948: * Makes a tile canal or water depending on the surroundings. frosch@8495: * frosch@8495: * Must only be used for converting old savegames. Use WaterClass now. frosch@8495: * rubidium@7948: * This as for example docks and shipdepots do not store rubidium@7948: * whether the tile used to be canal or 'normal' water. rubidium@7948: * @param t the tile to change. rubidium@7948: * @param o the owner of the new tile. frosch@9718: * @param include_invalid_water_class Also consider WATER_CLASS_INVALID, i.e. industry tiles on land rubidium@7948: */ frosch@9718: void SetWaterClassDependingOnSurroundings(TileIndex t, bool include_invalid_water_class) rubidium@7948: { frosch@9718: /* If the slope is not flat, we always assume 'land' (if allowed). Also for one-corner-raised-shores. frosch@9718: * Note: Wrt. autosloping under industry tiles this is the most fool-proof behaviour. */ frosch@9718: if (GetTileSlope(t, NULL) != SLOPE_FLAT) { frosch@9718: if (include_invalid_water_class) { frosch@9718: SetWaterClass(t, WATER_CLASS_INVALID); frosch@9718: return; frosch@9718: } else { frosch@9718: NOT_REACHED(); frosch@9718: } frosch@9718: } rubidium@7948: smatz@8022: /* Mark tile dirty in all cases */ smatz@8022: MarkTileDirtyByTile(t); smatz@8022: smatz@8526: if (TileX(t) == 0 || TileY(t) == 0 || TileX(t) == MapMaxX() - 1 || TileY(t) == MapMaxY() - 1) { smatz@8525: /* tiles at map borders are always WATER_CLASS_SEA */ smatz@8525: SetWaterClass(t, WATER_CLASS_SEA); smatz@8525: return; smatz@8525: } smatz@8525: rubidium@7948: bool has_water = false; rubidium@7948: bool has_canal = false; peter1138@8471: bool has_river = false; rubidium@7948: rubidium@7948: for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) { rubidium@7948: TileIndex neighbour = TileAddByDiagDir(t, dir); frosch@8414: switch (GetTileType(neighbour)) { frosch@8414: case MP_WATER: frosch@8495: /* clear water and shipdepots have already a WaterClass associated */ frosch@8495: if (IsCoast(neighbour)) { frosch@8495: has_water = true; frosch@8495: } else if (!IsLock(neighbour)) { frosch@8495: switch (GetWaterClass(neighbour)) { frosch@8495: case WATER_CLASS_SEA: has_water = true; break; frosch@8495: case WATER_CLASS_CANAL: has_canal = true; break; frosch@8495: case WATER_CLASS_RIVER: has_river = true; break; frosch@8495: default: NOT_REACHED(); frosch@8495: } frosch@8495: } frosch@8414: break; frosch@8414: frosch@8414: case MP_RAILWAY: frosch@8414: /* Shore or flooded halftile */ frosch@8414: has_water |= (GetRailGroundType(neighbour) == RAIL_GROUND_WATER); frosch@8414: break; frosch@8414: frosch@8459: case MP_TREES: frosch@8459: /* trees on shore */ frosch@8459: has_water |= (GetTreeGround(neighbour) == TREE_GROUND_SHORE); frosch@8459: break; frosch@8459: frosch@8414: default: break; rubidium@7948: } rubidium@7948: } peter1138@8471: frosch@9718: if (!has_water && !has_canal && !has_river && include_invalid_water_class) { frosch@9718: SetWaterClass(t, WATER_CLASS_INVALID); frosch@9718: return; frosch@9718: } frosch@9718: peter1138@8471: if (has_river && !has_canal) { peter1138@8471: SetWaterClass(t, WATER_CLASS_RIVER); peter1138@8471: } else if (has_canal || !has_water) { peter1138@8471: SetWaterClass(t, WATER_CLASS_CANAL); rubidium@7948: } else { peter1138@8471: SetWaterClass(t, WATER_CLASS_SEA); rubidium@7948: } rubidium@7948: } rubidium@7948: rubidium@7948: Darkvater@1784: /** Build a ship depot. tron@3491: * @param tile tile where ship depot is built belugas@6432: * @param flags type of operation tron@6134: * @param p1 bit 0 depot orientation (Axis) Darkvater@1784: * @param p2 unused truelight@0: */ rubidium@6943: CommandCost CmdBuildShipDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@3491: TileIndex tile2; truelight@193: rubidium@8230: CommandCost ret; truelight@0: tron@6134: Axis axis = Extract(p1); tron@6134: tron@6134: tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); truelight@0: peter1138@8471: if (!IsWaterTile(tile) || !IsWaterTile(tile2)) { truelight@0: return_cmd_error(STR_3801_MUST_BE_BUILT_ON_WATER); peter1138@8471: } truelight@0: celestar@5385: if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5385: peter1138@8471: if (GetTileSlope(tile, NULL) != SLOPE_FLAT || GetTileSlope(tile2, NULL) != SLOPE_FLAT) { peter1138@8471: /* Prevent depots on rapids */ peter1138@8471: return_cmd_error(STR_0239_SITE_UNSUITABLE); peter1138@8471: } peter1138@8471: peter1138@8471: WaterClass wc1 = GetWaterClass(tile); peter1138@8471: 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@9036: if (!Depot::CanAllocateItem()) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { rubidium@9036: Depot *depot = new Depot(tile); rubidium@10236: depot->town_index = ClosestTownFromTile(tile, UINT_MAX)->index; truelight@0: rubidium@10207: MakeShipDepot(tile, _current_company, DEPOT_NORTH, axis, wc1); rubidium@10207: MakeShipDepot(tile2, _current_company, DEPOT_SOUTH, axis, wc2); celestar@3372: MarkTileDirtyByTile(tile); celestar@3372: MarkTileDirtyByTile(tile2); truelight@0: } truelight@0: rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.build_ship_depot); truelight@0: } truelight@0: peter1138@8471: void MakeWaterKeepingClass(TileIndex tile, Owner o) rubidium@8029: { frosch@9718: assert(IsTileType(tile, MP_WATER) || (IsTileType(tile, MP_STATION) && (IsBuoy(tile) || IsDock(tile) || IsOilRig(tile))) || IsTileType(tile, MP_INDUSTRY)); peter1138@8471: frosch@9718: WaterClass wc = GetWaterClass(tile); frosch@9718: frosch@9718: /* Autoslope might turn an originally canal or river tile into land */ frosch@9718: uint z; frosch@9718: if (GetTileSlope(tile, &z) != SLOPE_FLAT) wc = WATER_CLASS_INVALID; frosch@9718: frosch@9718: if (wc == WATER_CLASS_SEA && z > 0) wc = WATER_CLASS_CANAL; frosch@9718: frosch@9718: switch (wc) { peter1138@8471: case WATER_CLASS_SEA: MakeWater(tile); break; peter1138@8471: case WATER_CLASS_CANAL: MakeCanal(tile, o, Random()); break; peter1138@8471: case WATER_CLASS_RIVER: MakeRiver(tile, Random()); break; frosch@9718: default: DoClearSquare(tile); break; rubidium@8029: } rubidium@8029: } rubidium@8029: rubidium@6943: 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@8520: TileIndex tile2 = GetOtherShipDepotTile(tile); truelight@0: smatz@8520: /* do not check for ship on tile when company goes bankrupt */ smatz@8520: if (!(flags & DC_BANKRUPT)) { smatz@8520: if (!EnsureNoVehicleOnGround(tile) || !EnsureNoVehicleOnGround(tile2)) return CMD_ERROR; smatz@8520: } truelight@0: truelight@0: if (flags & DC_EXEC) { celestar@3373: /* Kill the depot, which is registered at the northernmost tile. Use that one */ rubidium@7389: delete GetDepotByTile(tile2 < tile ? tile2 : tile); truelight@0: frosch@8495: MakeWaterKeepingClass(tile, GetTileOwner(tile)); frosch@8495: MakeWaterKeepingClass(tile2, GetTileOwner(tile2)); tron@3111: MarkTileDirtyByTile(tile); tron@3111: MarkTileDirtyByTile(tile2); truelight@0: } truelight@0: rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_ship_depot); truelight@0: } truelight@0: belugas@6432: /** build a shiplift */ rubidium@6943: static CommandCost DoBuildShiplift(TileIndex tile, DiagDirection dir, uint32 flags) truelight@0: { rubidium@6943: CommandCost ret; truelight@0: int delta; truelight@0: belugas@6432: /* 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@6432: /* lower tile */ peter1138@8471: WaterClass wc_lower = IsWaterTile(tile - delta) ? GetWaterClass(tile - delta) : WATER_CLASS_CANAL; peter1138@8471: 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@6432: /* upper tile */ peter1138@8471: WaterClass wc_upper = IsWaterTile(tile + delta) ? GetWaterClass(tile + delta) : WATER_CLASS_CANAL; peter1138@8471: 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@5385: if ((MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) || celestar@5385: (MayHaveBridgeAbove(tile - delta) && IsBridgeAbove(tile - delta)) || celestar@5385: (MayHaveBridgeAbove(tile + delta) && IsBridgeAbove(tile + delta))) { celestar@5385: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5385: } celestar@5385: truelight@0: if (flags & DC_EXEC) { rubidium@10207: MakeLock(tile, _current_company, dir, wc_lower, wc_upper); celestar@3372: MarkTileDirtyByTile(tile); celestar@3372: MarkTileDirtyByTile(tile - delta); celestar@3372: MarkTileDirtyByTile(tile + delta); frosch@8443: MarkCanalsAndRiversAroundDirty(tile - delta); frosch@8443: MarkCanalsAndRiversAroundDirty(tile + delta); truelight@0: } truelight@0: rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_water * 22 >> 3); truelight@0: } truelight@0: rubidium@6943: static CommandCost RemoveShiplift(TileIndex tile, uint32 flags) truelight@0: { Darkvater@4559: TileIndexDiff delta = TileOffsByDiagDir(GetLockDirection(tile)); truelight@0: rubidium@7270: if (!CheckTileOwnership(tile) && GetTileOwner(tile) != OWNER_NONE) return CMD_ERROR; peter1138@3940: belugas@6432: /* make sure no vehicle is on the tile. */ rubidium@7758: 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@8471: MakeWaterKeepingClass(tile + delta, GetTileOwner(tile)); peter1138@8471: MakeWaterKeepingClass(tile - delta, GetTileOwner(tile)); peter1138@8471: MarkTileDirtyByTile(tile - delta); peter1138@8471: MarkTileDirtyByTile(tile + delta); frosch@8443: MarkCanalsAndRiversAroundDirty(tile - delta); frosch@8443: MarkCanalsAndRiversAroundDirty(tile + delta); truelight@0: } truelight@193: rubidium@8230: 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@6432: * @param flags type of operation Darkvater@1796: * @param p1 unused Darkvater@1796: * @param p2 unused Darkvater@1796: */ rubidium@6943: CommandCost CmdBuildLock(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { frosch@8413: DiagDirection dir = GetInclinedSlopeDirection(GetTileSlope(tile, NULL)); frosch@8413: if (dir == INVALID_DIAGDIR) return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); peter1138@8386: peter1138@8386: /* Disallow building of locks on river rapids */ peter1138@8471: if (IsWaterTile(tile)) return_cmd_error(STR_0239_SITE_UNSUITABLE); peter1138@8386: 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@6432: * @param flags type of operation Darkvater@1796: * @param p1 start tile of stretch-dragging frosch@8413: * @param p2 specifies canal (0), water (1) or river (2); last two can only be built in scenario editor Darkvater@1796: */ rubidium@6943: CommandCost CmdBuildCanal(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@8230: 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@8360: rubidium@6286: /* Outside of the editor you can only build canals, not oceans */ peter1138@8361: 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@6106: if (x < sx) Swap(x, sx); tron@6106: 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@6943: CommandCost ret; celestar@5385: peter1138@8360: Slope slope = GetTileSlope(tile, NULL); frosch@8413: if (slope != SLOPE_FLAT && (p2 != 2 || !IsInclinedSlope(slope))) { tron@3636: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); tron@3636: } truelight@0: belugas@6432: /* can't make water of water! */ peter1138@8360: if (IsTileType(tile, MP_WATER) && (!IsTileOwner(tile, OWNER_WATER) || p2 == 1)) continue; Darkvater@1632: celestar@5385: ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); celestar@5385: if (CmdFailed(ret)) return ret; rubidium@6950: cost.AddCost(ret); tron@3183: tron@3189: if (flags & DC_EXEC) { peter1138@8360: if (TileHeight(tile) == 0 && p2 == 1) { celestar@5385: MakeWater(tile); peter1138@8360: } else if (p2 == 2) { peter1138@8368: MakeRiver(tile, Random()); celestar@5385: } else { rubidium@10207: MakeCanal(tile, _current_company, Random()); celestar@5385: } tron@3189: MarkTileDirtyByTile(tile); frosch@8443: MarkCanalsAndRiversAroundDirty(tile); tron@3189: } tron@3183: rubidium@6950: cost.AddCost(_price.clear_water); Darkvater@1632: } END_TILE_LOOP(tile, size_x, size_y, 0); truelight@0: rubidium@6950: 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@6943: static CommandCost ClearTile_Water(TileIndex tile, byte flags) tron@1977: { celestar@3425: switch (GetWaterTileType(tile)) { rubidium@6181: case WATER_TILE_CLEAR: celestar@3425: if (flags & DC_NO_WATER) return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); truelight@0: belugas@6432: /* Make sure it's not an edge tile. */ skidd13@7954: if (!IsInsideMM(TileX(tile), 1, MapMaxX() - 1) || skidd13@7954: !IsInsideMM(TileY(tile), 1, MapMaxY() - 1)) { celestar@3425: return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP); celestar@3425: } tron@3017: rubidium@7509: /* Make sure no vehicle is on the tile */ rubidium@7758: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; rubidium@7509: rubidium@7270: if (GetTileOwner(tile) != OWNER_WATER && GetTileOwner(tile) != OWNER_NONE && !CheckTileOwnership(tile)) return CMD_ERROR; peter1138@3940: frosch@8443: if (flags & DC_EXEC) { frosch@8443: DoClearSquare(tile); frosch@8443: MarkCanalsAndRiversAroundDirty(tile); frosch@8443: } rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_water); truelight@0: rubidium@6181: case WATER_TILE_COAST: { tron@3636: Slope slope = GetTileSlope(tile, NULL); tron@3439: belugas@6432: /* Make sure no vehicle is on the tile */ rubidium@7758: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; tron@3439: frosch@8443: if (flags & DC_EXEC) { frosch@8443: DoClearSquare(tile); frosch@8443: MarkCanalsAndRiversAroundDirty(tile); frosch@8443: } frosch@8413: if (IsSlopeWithOneCornerRaised(slope)) { rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_water); tron@3439: } else { rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.clear_roughland); celestar@3425: } tron@3439: } tron@3439: rubidium@6181: 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); rubidium@10207: if (_current_company == OWNER_WATER) return CMD_ERROR; belugas@6432: /* move to the middle tile.. */ tron@3439: return RemoveShiplift(tile + ToTileIndexDiff(_shiplift_tomiddle_offs[GetSection(tile)]), flags); tron@3439: } tron@3439: rubidium@6181: 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@8441: /** frosch@8441: * return true if a tile is a water tile wrt. a certain direction. frosch@8441: * frosch@8441: * @param tile The tile of interest. frosch@8441: * @param from The direction of interest. frosch@8441: * @return true iff the tile is water in the view of 'from'. frosch@8441: * frosch@8441: */ frosch@8441: static bool IsWateredTile(TileIndex tile, Direction from) truelight@0: { tron@1214: switch (GetTileType(tile)) { tron@3977: case MP_WATER: rubidium@9496: switch (GetWaterTileType(tile)) { rubidium@9496: default: NOT_REACHED(); rubidium@9496: case WATER_TILE_DEPOT: case WATER_TILE_CLEAR: return true; rubidium@9496: case WATER_TILE_LOCK: return DiagDirToAxis(GetLockDirection(tile)) == DiagDirToAxis(DirToDiagDir(from)); rubidium@9496: rubidium@9496: case WATER_TILE_COAST: rubidium@9496: switch (GetTileSlope(tile, NULL)) { rubidium@9496: case SLOPE_W: return (from == DIR_SE) || (from == DIR_E) || (from == DIR_NE); rubidium@9496: case SLOPE_S: return (from == DIR_NE) || (from == DIR_N) || (from == DIR_NW); rubidium@9496: case SLOPE_E: return (from == DIR_NW) || (from == DIR_W) || (from == DIR_SW); rubidium@9496: case SLOPE_N: return (from == DIR_SW) || (from == DIR_S) || (from == DIR_SE); rubidium@9496: default: return false; rubidium@9496: } frosch@8441: } tron@3977: frosch@8414: case MP_RAILWAY: frosch@8414: if (GetRailGroundType(tile) == RAIL_GROUND_WATER) { frosch@8414: assert(IsPlainRailTile(tile)); frosch@8441: switch (GetTileSlope(tile, NULL)) { frosch@8441: case SLOPE_W: return (from == DIR_SE) || (from == DIR_E) || (from == DIR_NE); frosch@8441: case SLOPE_S: return (from == DIR_NE) || (from == DIR_N) || (from == DIR_NW); frosch@8441: case SLOPE_E: return (from == DIR_NW) || (from == DIR_W) || (from == DIR_SW); frosch@8441: case SLOPE_N: return (from == DIR_SW) || (from == DIR_S) || (from == DIR_SE); frosch@8441: default: return false; frosch@8441: } frosch@8414: } frosch@8414: return false; frosch@8414: frosch@9721: case MP_STATION: frosch@9722: if (IsOilRig(tile)) { frosch@9722: /* Do not draw waterborders inside of industries. frosch@9722: * Note: There is no easy way to detect the industry of an oilrig tile. */ frosch@9722: TileIndex src_tile = tile + TileOffsByDir(from); frosch@9722: if ((IsTileType(src_tile, MP_STATION) && IsOilRig(src_tile)) || frosch@9722: (IsTileType(src_tile, MP_INDUSTRY))) return true; frosch@9722: frosch@9722: return GetWaterClass(tile) != WATER_CLASS_INVALID; frosch@9722: } frosch@9721: return (IsDock(tile) && GetTileSlope(tile, NULL) == SLOPE_FLAT) || IsBuoy(tile); frosch@9721: frosch@9722: case MP_INDUSTRY: { frosch@9722: /* Do not draw waterborders inside of industries. frosch@9722: * Note: There is no easy way to detect the industry of an oilrig tile. */ frosch@9722: TileIndex src_tile = tile + TileOffsByDir(from); frosch@9722: if ((IsTileType(src_tile, MP_STATION) && IsOilRig(src_tile)) || frosch@9722: (IsTileType(src_tile, MP_INDUSTRY) && GetIndustryIndex(src_tile) == GetIndustryIndex(tile))) return true; frosch@9722: frosch@9722: return IsIndustryTileOnWater(tile); frosch@9722: } frosch@9722: rubidium@9496: case MP_TUNNELBRIDGE: return GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER && ReverseDiagDir(GetTunnelBridgeDirection(tile)) == DirToDiagDir(from); frosch@9722: rubidium@7507: default: return false; tron@1048: } truelight@0: } truelight@0: peter1138@8360: static void DrawWaterEdges(SpriteID base, TileIndex tile) truelight@0: { truelight@0: uint wa; truelight@193: belugas@6432: /* determine the edges around with water. */ frosch@8441: wa = IsWateredTile(TILE_ADDXY(tile, -1, 0), DIR_SW) << 0; frosch@8441: wa += IsWateredTile(TILE_ADDXY(tile, 0, 1), DIR_NW) << 1; frosch@8441: wa += IsWateredTile(TILE_ADDXY(tile, 1, 0), DIR_NE) << 2; frosch@8441: wa += IsWateredTile(TILE_ADDXY(tile, 0, -1), DIR_SE) << 3; truelight@193: peter1138@8360: if (!(wa & 1)) DrawGroundSprite(base, PAL_NONE); peter1138@8360: if (!(wa & 2)) DrawGroundSprite(base + 1, PAL_NONE); peter1138@8360: if (!(wa & 4)) DrawGroundSprite(base + 2, PAL_NONE); peter1138@8360: if (!(wa & 8)) DrawGroundSprite(base + 3, PAL_NONE); truelight@0: belugas@6432: /* right corner */ tron@2989: switch (wa & 0x03) { peter1138@8360: case 0: DrawGroundSprite(base + 4, PAL_NONE); break; frosch@8441: case 3: if (!IsWateredTile(TILE_ADDXY(tile, -1, 1), DIR_W)) DrawGroundSprite(base + 8, PAL_NONE); break; tron@2989: } truelight@0: belugas@6432: /* bottom corner */ tron@2989: switch (wa & 0x06) { peter1138@8360: case 0: DrawGroundSprite(base + 5, PAL_NONE); break; frosch@8441: case 6: if (!IsWateredTile(TILE_ADDXY(tile, 1, 1), DIR_N)) DrawGroundSprite(base + 9, PAL_NONE); break; tron@2989: } truelight@0: belugas@6432: /* left corner */ tron@2989: switch (wa & 0x0C) { peter1138@8360: case 0: DrawGroundSprite(base + 6, PAL_NONE); break; frosch@8441: case 12: if (!IsWateredTile(TILE_ADDXY(tile, 1, -1), DIR_E)) DrawGroundSprite(base + 10, PAL_NONE); break; tron@2989: } truelight@0: belugas@6432: /* upper corner */ tron@2989: switch (wa & 0x09) { peter1138@8360: case 0: DrawGroundSprite(base + 7, PAL_NONE); break; frosch@8441: case 9: if (!IsWateredTile(TILE_ADDXY(tile, -1, -1), DIR_S)) DrawGroundSprite(base + 11, PAL_NONE); break; tron@2989: } truelight@0: } truelight@0: peter1138@8471: /** Draw a plain sea water tile with no edges */ frosch@8496: static void DrawSeaWater(TileIndex tile) peter1138@8471: { peter1138@8471: DrawGroundSprite(SPR_FLAT_WATER_TILE, PAL_NONE); peter1138@8471: } peter1138@8471: peter1138@8360: /** draw a canal styled water tile with dikes around */ frosch@8496: static void DrawCanalWater(TileIndex tile) peter1138@8360: { frosch@8496: DrawGroundSprite(SPR_FLAT_WATER_TILE, PAL_NONE); peter1138@8471: peter1138@8360: /* Test for custom graphics, else use the default */ peter1138@8360: SpriteID dikes_base = GetCanalSprite(CF_DIKES, tile); peter1138@8360: if (dikes_base == 0) dikes_base = SPR_CANAL_DIKES_BASE; peter1138@8360: peter1138@8360: DrawWaterEdges(dikes_base, tile); peter1138@8360: } peter1138@8360: rubidium@6248: struct LocksDrawTileStruct { truelight@0: int8 delta_x, delta_y, delta_z; truelight@0: byte width, height, depth; truelight@0: SpriteID image; rubidium@6248: }; truelight@0: truelight@0: #include "table/water_land.h" truelight@0: Darkvater@2436: static void DrawWaterStuff(const TileInfo *ti, const WaterDrawTileStruct *wdts, frosch@8496: SpriteID palette, uint base, bool draw_ground tron@1399: ) truelight@0: { peter1138@6583: SpriteID image; peter1138@6583: SpriteID water_base = GetCanalSprite(CF_WATERSLOPE, ti->tile); peter1138@6583: SpriteID locks_base = GetCanalSprite(CF_LOCKS, ti->tile); peter1138@6583: peter1138@6583: /* If no custom graphics, use defaults */ rubidium@7882: if (water_base == 0) water_base = SPR_CANALS_BASE; peter1138@6583: if (locks_base == 0) { rubidium@7882: locks_base = SPR_SHIPLIFT_BASE; peter1138@6583: } else { peter1138@6583: /* If using custom graphics, ignore the variation on height */ peter1138@6583: base = 0; peter1138@6583: } peter1138@6583: peter1138@6583: image = wdts++->image; peter1138@6583: if (image < 4) image += water_base; frosch@8496: if (draw_ground) DrawGroundSprite(image, PAL_NONE); truelight@193: smatz@8806: /* End now if buildings are invisible */ smatz@8806: if (IsInvisibilitySet(TO_BUILDINGS)) return; smatz@8806: tron@1399: for (; wdts->delta_x != 0x80; wdts++) { rubidium@7333: AddSortableSpriteToDraw(wdts->image + base + ((wdts->image < 24) ? locks_base : 0), palette, peter1138@5668: ti->x + wdts->delta_x, ti->y + wdts->delta_y, peter1138@5668: wdts->width, wdts->height, rubidium@7333: wdts->unk, ti->z + wdts->delta_z, belugas@7849: IsTransparencySet(TO_BUILDINGS)); truelight@0: } truelight@0: } truelight@0: frosch@8496: static void DrawRiverWater(const TileInfo *ti) peter1138@8360: { peter1138@8360: SpriteID image = SPR_FLAT_WATER_TILE; peter1138@8360: SpriteID edges_base = GetCanalSprite(CF_RIVER_EDGE, ti->tile); peter1138@8360: peter1138@8360: if (ti->tileh != SLOPE_FLAT) { peter1138@8360: image = GetCanalSprite(CF_RIVER_SLOPE, ti->tile); peter1138@8360: if (image == 0) { frosch@8430: switch (ti->tileh) { frosch@8430: case SLOPE_NW: image = SPR_WATER_SLOPE_Y_DOWN; break; frosch@8430: case SLOPE_SW: image = SPR_WATER_SLOPE_X_UP; break; frosch@8430: case SLOPE_SE: image = SPR_WATER_SLOPE_Y_UP; break; frosch@8430: case SLOPE_NE: image = SPR_WATER_SLOPE_X_DOWN; break; frosch@8430: default: image = SPR_FLAT_WATER_TILE; break; frosch@8430: } peter1138@8360: } else { peter1138@8360: switch (ti->tileh) { peter1138@8360: default: NOT_REACHED(); peter1138@8360: case SLOPE_SE: edges_base += 12; break; peter1138@8360: case SLOPE_NE: image += 1; edges_base += 24; break; peter1138@8360: case SLOPE_SW: image += 2; edges_base += 36; break; peter1138@8360: case SLOPE_NW: image += 3; edges_base += 48; break; peter1138@8360: } peter1138@8360: } peter1138@8360: } peter1138@8360: frosch@8496: DrawGroundSprite(image, PAL_NONE); peter1138@8360: frosch@8430: /* Draw river edges if available. */ frosch@8430: if (edges_base > 48) DrawWaterEdges(edges_base, ti->tile); peter1138@8360: } peter1138@8360: frosch@8380: void DrawShoreTile(Slope tileh) frosch@8380: { frosch@8380: /* Converts the enum Slope into an offset based on SPR_SHORE_BASE. frosch@8380: * This allows to calculate the proper sprite to display for this Slope */ frosch@8380: static const byte tileh_to_shoresprite[32] = { frosch@8380: 0, 1, 2, 3, 4, 16, 6, 7, 8, 9, 17, 11, 12, 13, 14, 0, frosch@8380: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 10, 15, 0, frosch@8380: }; frosch@8380: frosch@8380: assert(!IsHalftileSlope(tileh)); // Halftile slopes need to get handled earlier. frosch@8380: assert(tileh != SLOPE_FLAT); // Shore is never flat frosch@8380: frosch@8380: assert((tileh != SLOPE_EW) && (tileh != SLOPE_NS)); // No suitable sprites for current flooding behaviour frosch@8380: frosch@8380: DrawGroundSprite(SPR_SHORE_BASE + tileh_to_shoresprite[tileh], PAL_NONE); frosch@8380: } frosch@8380: frosch@8496: void DrawWaterClassGround(const TileInfo *ti) { frosch@8496: switch (GetWaterClass(ti->tile)) { frosch@8496: case WATER_CLASS_SEA: DrawSeaWater(ti->tile); break; frosch@8496: case WATER_CLASS_CANAL: DrawCanalWater(ti->tile); break; frosch@8496: case WATER_CLASS_RIVER: DrawRiverWater(ti); break; frosch@9718: default: NOT_REACHED(); frosch@8496: } frosch@8496: } frosch@8496: truelight@0: static void DrawTile_Water(TileInfo *ti) truelight@0: { celestar@3402: switch (GetWaterTileType(ti->tile)) { rubidium@6181: case WATER_TILE_CLEAR: frosch@8496: DrawWaterClassGround(ti); celestar@5385: DrawBridgeMiddle(ti); celestar@3402: break; truelight@0: belugas@8164: case WATER_TILE_COAST: { frosch@8380: DrawShoreTile(ti->tileh); celestar@5385: DrawBridgeMiddle(ti); belugas@8164: } break; truelight@0: rubidium@6181: case WATER_TILE_LOCK: { celestar@3425: const WaterDrawTileStruct *t = _shiplift_display_seq[GetSection(ti->tile)]; frosch@8496: DrawWaterStuff(ti, t, 0, ti->z > t[3].delta_y ? 24 : 0, true); celestar@3402: } break; celestar@3402: rubidium@6181: case WATER_TILE_DEPOT: frosch@8496: DrawWaterClassGround(ti); rubidium@10207: DrawWaterStuff(ti, _shipdepot_display_seq[GetSection(ti->tile)], COMPANY_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@5668: 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); rubidium@10207: DrawSprite(wdts->image, COMPANY_SPRITE_COLOR(_local_company), 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@5587: Slope tileh = GetTileSlope(tile, &z); tron@4231: tron@4231: return z + GetPartialZ(x & 0xF, y & 0xF, tileh); truelight@0: } truelight@0: rubidium@7335: static Foundation GetFoundation_Water(TileIndex tile, Slope tileh) truelight@193: { rubidium@7335: 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@6181: case WATER_TILE_CLEAR: belugas@9517: switch (GetWaterClass(tile)) { belugas@9517: case WATER_CLASS_SEA: td->str = STR_3804_WATER; break; belugas@9517: case WATER_CLASS_CANAL: td->str = STR_LANDINFO_CANAL; break; belugas@9517: case WATER_CLASS_RIVER: td->str = STR_LANDINFO_RIVER; break; belugas@9517: default: assert(0); break; celestar@3402: } celestar@3402: break; rubidium@6181: case WATER_TILE_COAST: td->str = STR_3805_COAST_OR_RIVERBANK; break; belugas@9517: case WATER_TILE_LOCK : td->str = STR_LANDINFO_LOCK; break; belugas@9517: case WATER_TILE_DEPOT: td->str = STR_3806_SHIP_DEPOT; break; celestar@3402: default: assert(0); break; tron@2951: } truelight@0: frosch@9322: td->owner[0] = GetTileOwner(tile); truelight@0: } truelight@0: tron@1977: static void AnimateTile_Water(TileIndex tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: rubidium@10083: static void FloodVehicle(Vehicle *v); rubidium@10083: rubidium@10083: /** rubidium@10083: * Flood a vehicle if we are allowed to flood it, i.e. when it is on the ground. rubidium@10083: * @param v The vehicle to test for flooding. rubidium@10083: * @param data The z of level to flood. rubidium@10083: * @return NULL as we always want to remove everything. rubidium@10083: */ rubidium@10083: static Vehicle *FloodVehicleProc(Vehicle *v, void *data) rubidium@10083: { rubidium@10083: byte z = *(byte*)data; rubidium@10083: rubidium@10083: if (v->type == VEH_DISASTER || (v->type == VEH_AIRCRAFT && v->subtype == AIR_SHADOW)) return NULL; rubidium@10083: if (v->z_pos > z || (v->vehstatus & VS_CRASHED) != 0) return NULL; rubidium@10083: rubidium@10083: FloodVehicle(v); rubidium@10083: return NULL; rubidium@10083: } truelight@0: rubidium@5940: /** rubidium@5940: * Finds a vehicle to flood. rubidium@5940: * It does not find vehicles that are already crashed on bridges, i.e. flooded. rubidium@5940: * @param tile the tile where to find a vehicle to flood rubidium@5940: */ rubidium@10083: static void FloodVehicles(TileIndex tile) rubidium@5940: { rubidium@10083: byte z = 0; rubidium@10083: rubidium@6906: if (IsTileType(tile, MP_STATION) && IsAirport(tile)) { rubidium@6906: const Station *st = GetStationByTile(tile); rubidium@6906: const AirportFTAClass *airport = st->Airport(); rubidium@10083: z = 1 + airport->delta_z; rubidium@6906: for (uint x = 0; x < airport->size_x; x++) { rubidium@6906: for (uint y = 0; y < airport->size_y; y++) { rubidium@6906: tile = TILE_ADDXY(st->airport_tile, x, y); rubidium@10083: FindVehicleOnPos(tile, &z, &FloodVehicleProc); rubidium@6906: } rubidium@6906: } rubidium@6906: rubidium@6906: /* No vehicle could be flooded on this airport anymore */ rubidium@10083: return; rubidium@6906: } rubidium@6906: smatz@8014: /* if non-uniform stations are disabled, flood some train in this train station (if there is any) */ rubidium@9413: if (!_settings_game.station.nonuniform_stations && IsTileType(tile, MP_STATION) && GetStationType(tile) == STATION_RAIL) { smatz@8014: const Station *st = GetStationByTile(tile); smatz@8014: smatz@8014: BEGIN_TILE_LOOP(t, st->trainst_w, st->trainst_h, st->train_tile) smatz@8014: if (st->TileBelongsToRailStation(t)) { rubidium@10083: FindVehicleOnPos(tile, &z, &FloodVehicleProc); smatz@8014: } smatz@8014: END_TILE_LOOP(t, st->trainst_w, st->trainst_h, st->train_tile) smatz@8014: rubidium@10083: return; smatz@8014: } smatz@8014: rubidium@10083: if (!IsBridgeTile(tile)) { rubidium@10083: FindVehicleOnPos(tile, &z, &FloodVehicleProc); rubidium@10083: return; rubidium@10083: } rubidium@5940: rubidium@5940: TileIndex end = GetOtherBridgeEnd(tile); rubidium@10083: z = GetBridgeHeight(tile); rubidium@5940: rubidium@10083: FindVehicleOnPos(tile, &z, &FloodVehicleProc); rubidium@10083: FindVehicleOnPos(end, &z, &FloodVehicleProc); rubidium@5940: } rubidium@5940: darkvater@149: static void FloodVehicle(Vehicle *v) darkvater@149: { darkvater@149: if (!(v->vehstatus & VS_CRASHED)) { darkvater@168: uint16 pass = 0; darkvater@149: rubidium@6906: if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_AIRCRAFT) { rubidium@6906: if (v->type == VEH_AIRCRAFT) { rubidium@6906: /* Crashing aircraft are always at z_pos == 1, never on z_pos == 0, rubidium@6906: * because that's always the shadow. Except for the heliport, because rubidium@6906: * that station has a big z_offset for the aircraft. */ rubidium@6906: if (!IsTileType(v->tile, MP_STATION) || !IsAirport(v->tile) || GetTileMaxZ(v->tile) != 0) return; rubidium@6906: const Station *st = GetStationByTile(v->tile); rubidium@6906: const AirportFTAClass *airport = st->Airport(); rubidium@6906: rubidium@6906: if (v->z_pos != airport->delta_z + 1) return; rubidium@6906: } truelight@193: rubidium@7497: if (v->type != VEH_AIRCRAFT) v = v->First(); darkvater@149: maedhros@6857: /* crash all wagons, and count passengers */ peter1138@9321: for (Vehicle *u = v; u != NULL; u = u->Next()) { peter1138@9321: if (IsCargoInClass(u->cargo_type, CC_PASSENGERS)) pass += u->cargo.Count(); peter1138@9321: u->vehstatus |= VS_CRASHED; peter1138@9321: MarkSingleVehicleDirty(u); peter1138@9321: } maedhros@6857: rubidium@6906: switch (v->type) { rubidium@6906: default: NOT_REACHED(); rubidium@6906: case VEH_TRAIN: rubidium@9811: if (IsFrontEngine(v)) { rubidium@9811: pass += 4; // driver smatz@9948: /* FreeTrainTrackReservation() calls GetVehicleTrackdir() that doesn't like crashed vehicles. smatz@9948: * In this case, v->direction matches v->u.rail.track, so we can do this (it wasn't crashed before) */ smatz@9948: v->vehstatus &= ~VS_CRASHED; rubidium@9811: FreeTrainTrackReservation(v); smatz@9948: v->vehstatus |= VS_CRASHED; rubidium@9811: } rubidium@6906: v->u.rail.crash_anim_pos = 4000; // max 4440, disappear pretty fast rubidium@9297: InvalidateWindowClassesData(WC_TRAINS_LIST, 0); rubidium@6906: break; rubidium@6906: rubidium@6906: case VEH_ROAD: rubidium@6906: if (IsRoadVehFront(v)) pass += 1; // driver rubidium@6906: v->u.road.crashed_ctr = 2000; // max 2220, disappear pretty fast rubidium@9297: InvalidateWindowClassesData(WC_ROADVEH_LIST, 0); rubidium@6906: break; rubidium@6906: rubidium@6906: case VEH_AIRCRAFT: rubidium@6906: pass += 2; // driver rubidium@6906: v->u.air.crashed_counter = 9000; // max 10000, disappear pretty fast rubidium@9297: InvalidateWindowClassesData(WC_AIRCRAFT_LIST, 0); rubidium@6906: break; maedhros@6857: } tron@2549: } else { darkvater@168: return; tron@2549: } darkvater@149: smatz@8350: 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@9234: NS_ACCIDENT_VEHICLE, 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@7731: /** frosch@8380: * Returns the behaviour of a tile during flooding. frosch@8380: * frosch@8380: * @return Behaviour of the tile frosch@8380: */ frosch@8380: static FloodingBehaviour GetFloodingBehaviour(TileIndex tile) frosch@8380: { frosch@9718: /* FLOOD_ACTIVE: 'single-corner-raised'-coast, sea, sea-shipdepots, sea-buoys, sea-docks (water part), rail with flooded halftile, sea-water-industries, sea-oilrigs frosch@8459: * FLOOD_DRYUP: coast with more than one corner raised, coast with rail-track, coast with trees frosch@9718: * FLOOD_PASSIVE: (not used) frosch@8380: * FLOOD_NONE: canals, rivers, everything else frosch@8380: */ frosch@8380: switch (GetTileType(tile)) { frosch@8380: case MP_WATER: frosch@8380: if (IsCoast(tile)) { frosch@8380: Slope tileh = GetTileSlope(tile, NULL); frosch@8413: return (IsSlopeWithOneCornerRaised(tileh) ? FLOOD_ACTIVE : FLOOD_DRYUP); frosch@8380: } else { peter1138@8471: return (GetWaterClass(tile) == WATER_CLASS_SEA) ? FLOOD_ACTIVE : FLOOD_NONE; frosch@8380: } frosch@8380: frosch@8380: case MP_RAILWAY: frosch@8414: if (GetRailGroundType(tile) == RAIL_GROUND_WATER) { frosch@8414: return (IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL)) ? FLOOD_ACTIVE : FLOOD_DRYUP); frosch@8414: } frosch@8414: return FLOOD_NONE; frosch@8380: frosch@8459: case MP_TREES: frosch@8459: return (GetTreeGround(tile) == TREE_GROUND_SHORE ? FLOOD_DRYUP : FLOOD_NONE); frosch@8459: frosch@8380: case MP_STATION: frosch@9718: if (IsBuoy(tile) || (IsDock(tile) && GetTileSlope(tile, NULL) == SLOPE_FLAT) || IsOilRig(tile)) { frosch@8497: return (GetWaterClass(tile) == WATER_CLASS_SEA ? FLOOD_ACTIVE : FLOOD_NONE); frosch@8497: } frosch@9718: return FLOOD_NONE; frosch@8380: frosch@8380: case MP_INDUSTRY: frosch@9718: return ((IsIndustryTileOnWater(tile) && GetWaterClass(tile) == WATER_CLASS_SEA) ? FLOOD_ACTIVE : FLOOD_NONE); frosch@8380: frosch@8380: default: frosch@8380: return FLOOD_NONE; frosch@8380: } frosch@8380: } frosch@8380: frosch@8380: /** frosch@8380: * Floods a tile. frosch@8380: */ frosch@8380: static void DoFloodTile(TileIndex target) frosch@8380: { glx@8684: assert(!IsTileType(target, MP_WATER)); frosch@8380: frosch@8380: bool flooded = false; // Will be set to true if something is changed. frosch@8380: rubidium@10207: _current_company = OWNER_WATER; frosch@8380: frosch@8459: Slope tileh = GetTileSlope(target, NULL); frosch@8459: if (tileh != SLOPE_FLAT) { frosch@8380: /* make coast.. */ frosch@8380: switch (GetTileType(target)) { frosch@8380: case MP_RAILWAY: { frosch@8380: if (!IsPlainRailTile(target)) break; rubidium@10083: FloodVehicles(target); frosch@8380: flooded = FloodHalftile(target); frosch@8380: break; frosch@8380: } frosch@8380: frosch@8459: case MP_TREES: frosch@8459: if (!IsSlopeWithOneCornerRaised(tileh)) { frosch@8459: SetTreeGroundDensity(target, TREE_GROUND_SHORE, 3); frosch@8459: MarkTileDirtyByTile(target); frosch@8459: flooded = true; frosch@8459: break; frosch@8459: } frosch@8459: /* FALL THROUGH */ frosch@8380: case MP_CLEAR: frosch@8380: if (CmdSucceeded(DoCommand(target, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) { frosch@8380: MakeShore(target); frosch@8380: MarkTileDirtyByTile(target); frosch@8380: flooded = true; frosch@8380: } frosch@8380: break; frosch@8380: frosch@8380: default: frosch@8380: break; frosch@8380: } frosch@8380: } else { frosch@8380: /* Flood vehicles */ rubidium@10083: FloodVehicles(target); frosch@8380: frosch@8380: /* flood flat tile */ frosch@8380: if (CmdSucceeded(DoCommand(target, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) { frosch@8380: MakeWater(target); frosch@8380: MarkTileDirtyByTile(target); frosch@8380: flooded = true; frosch@8380: } frosch@8380: } frosch@8380: frosch@8380: if (flooded) { frosch@8380: /* Mark surrounding canal tiles dirty too to avoid glitches */ frosch@8443: MarkCanalsAndRiversAroundDirty(target); frosch@8380: frosch@8380: /* update signals if needed */ frosch@8380: UpdateSignalsInBuffer(); frosch@8380: } frosch@8380: rubidium@10207: _current_company = OWNER_NONE; frosch@8380: } frosch@8380: frosch@8380: /** frosch@8380: * Drys a tile up. frosch@8380: */ frosch@8380: static void DoDryUp(TileIndex tile) frosch@8380: { rubidium@10207: _current_company = OWNER_WATER; frosch@8380: frosch@8414: switch (GetTileType(tile)) { frosch@8414: case MP_RAILWAY: frosch@8414: assert(IsPlainRailTile(tile)); frosch@8414: assert(GetRailGroundType(tile) == RAIL_GROUND_WATER); frosch@8414: frosch@8414: RailGroundType new_ground; frosch@8414: switch (GetTrackBits(tile)) { frosch@8414: case TRACK_BIT_UPPER: new_ground = RAIL_GROUND_FENCE_HORIZ1; break; frosch@8414: case TRACK_BIT_LOWER: new_ground = RAIL_GROUND_FENCE_HORIZ2; break; frosch@8414: case TRACK_BIT_LEFT: new_ground = RAIL_GROUND_FENCE_VERT1; break; frosch@8414: case TRACK_BIT_RIGHT: new_ground = RAIL_GROUND_FENCE_VERT2; break; frosch@8414: default: NOT_REACHED(); frosch@8414: } frosch@8414: SetRailGroundType(tile, new_ground); frosch@8414: MarkTileDirtyByTile(tile); frosch@8414: break; frosch@8414: frosch@8459: case MP_TREES: frosch@8459: SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3); frosch@8459: MarkTileDirtyByTile(tile); frosch@8459: break; frosch@8459: frosch@8414: case MP_WATER: frosch@8414: assert(IsCoast(tile)); frosch@8414: frosch@8414: if (CmdSucceeded(DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR))) { frosch@8414: MakeClear(tile, CLEAR_GRASS, 3); frosch@8414: MarkTileDirtyByTile(tile); frosch@8414: } frosch@8414: break; frosch@8414: frosch@8414: default: NOT_REACHED(); frosch@8380: } frosch@8380: rubidium@10207: _current_company = OWNER_NONE; frosch@8380: } frosch@8380: frosch@8380: /** belugas@7731: * Let a water tile floods its diagonal adjoining tiles rubidium@7771: * called from tunnelbridge_cmd, and by TileLoop_Industry() and TileLoop_Track() belugas@7731: * belugas@7731: * @param tile the water/shore tile that floods belugas@7731: */ tron@1977: void TileLoop_Water(TileIndex tile) truelight@0: { frosch@8380: switch (GetFloodingBehaviour(tile)) { frosch@8380: case FLOOD_ACTIVE: frosch@8380: for (Direction dir = DIR_BEGIN; dir < DIR_END; dir++) { frosch@8380: TileIndex dest = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDir(dir)); frosch@8380: if (dest == INVALID_TILE) continue; glx@8684: /* do not try to flood water tiles - increases performance a lot */ glx@8684: if (IsTileType(dest, MP_WATER)) continue; tron@2989: frosch@8380: uint z_dest; frosch@8801: Slope slope_dest = GetFoundationSlope(dest, &z_dest) & ~SLOPE_HALFTILE_MASK & ~SLOPE_STEEP; frosch@8380: if (z_dest > 0) continue; dominik@43: frosch@8380: if (!HasBit(_flood_from_dirs[slope_dest], ReverseDir(dir))) continue; dominik@43: frosch@8380: DoFloodTile(dest); frosch@8380: } frosch@8380: break; dominik@43: frosch@8380: case FLOOD_DRYUP: { frosch@8801: Slope slope_here = GetFoundationSlope(tile, NULL) & ~SLOPE_HALFTILE_MASK & ~SLOPE_STEEP; frosch@8380: uint check_dirs = _flood_from_dirs[slope_here]; frosch@8380: uint dir; frosch@8380: FOR_EACH_SET_BIT(dir, check_dirs) { frosch@8380: TileIndex dest = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDir((Direction)dir)); frosch@8380: if (dest == INVALID_TILE) continue; frosch@8380: frosch@8380: FloodingBehaviour dest_behaviour = GetFloodingBehaviour(dest); frosch@8380: if ((dest_behaviour == FLOOD_ACTIVE) || (dest_behaviour == FLOOD_PASSIVE)) return; frosch@8380: } frosch@8380: DoDryUp(tile); frosch@8380: break; frosch@8380: } frosch@8380: frosch@8380: default: return; tron@2639: } frosch@8380: } dominik@43: frosch@8380: void ConvertGroundTilesIntoWaterTiles() frosch@8380: { frosch@8380: TileIndex tile; frosch@8380: uint z; frosch@8380: Slope slope; frosch@8380: frosch@8380: for (tile = 0; tile < MapSize(); ++tile) { frosch@8380: slope = GetTileSlope(tile, &z); frosch@8380: if (IsTileType(tile, MP_CLEAR) && z == 0) { frosch@8380: /* Make both water for tiles at level 0 frosch@8380: * and make shore, as that looks much better frosch@8380: * during the generation. */ frosch@8380: switch (slope) { frosch@8380: case SLOPE_FLAT: frosch@8380: MakeWater(tile); frosch@8380: break; frosch@8380: frosch@8380: case SLOPE_N: frosch@8380: case SLOPE_E: frosch@8380: case SLOPE_S: frosch@8380: case SLOPE_W: frosch@8380: MakeShore(tile); frosch@8380: break; frosch@8380: frosch@8380: default: frosch@8380: uint check_dirs = _flood_from_dirs[slope & ~SLOPE_STEEP]; frosch@8380: uint dir; frosch@8380: FOR_EACH_SET_BIT(dir, check_dirs) { frosch@8380: TileIndex dest = TILE_ADD(tile, TileOffsByDir((Direction)dir)); frosch@8801: Slope slope_dest = GetTileSlope(dest, NULL) & ~SLOPE_STEEP; frosch@8413: if (slope_dest == SLOPE_FLAT || IsSlopeWithOneCornerRaised(slope_dest)) { frosch@8380: MakeShore(tile); frosch@8380: break; frosch@8380: } frosch@8380: } frosch@8380: break; frosch@8380: } frosch@8380: } tron@2639: } truelight@0: } truelight@0: frosch@8616: static TrackStatus GetTileTrackStatus_Water(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side) 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@8471: case WATER_TILE_CLEAR: ts = (GetTileSlope(tile, NULL) == SLOPE_FLAT) ? TRACK_BIT_ALL : TRACK_BIT_NONE; break; rubidium@6181: case WATER_TILE_COAST: ts = (TrackBits)coast_tracks[GetTileSlope(tile, NULL) & 0xF]; break; smatz@9224: case WATER_TILE_LOCK: ts = DiagDirToDiagTrackBits(GetLockDirection(tile)); break; rubidium@6181: case WATER_TILE_DEPOT: ts = AxisToTrackBits(GetShipDepotAxis(tile)); break; celestar@3423: default: return 0; truelight@0: } KUDr@3900: if (TileX(tile) == 0) { belugas@6432: /* 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@6432: /* NW border: remove tracks that connects NW tile edge */ KUDr@3900: ts &= ~(TRACK_BIT_Y | TRACK_BIT_LEFT | TRACK_BIT_UPPER); KUDr@3900: } frosch@8616: return CombineTrackStatus(TrackBitsToTrackdirBits(ts), TRACKDIR_BIT_NONE); truelight@0: } truelight@0: tron@1977: static void ClickTile_Water(TileIndex tile) truelight@0: { rubidium@6181: if (GetWaterTileType(tile) == WATER_TILE_DEPOT) { celestar@3423: TileIndex tile2 = GetOtherShipDepotTile(tile); truelight@193: rubidium@6259: ShowDepotWindow(tile < tile2 ? tile : tile2, VEH_SHIP); truelight@0: } truelight@0: } truelight@0: rubidium@10207: static void ChangeTileOwner_Water(TileIndex tile, Owner old_owner, Owner new_owner) truelight@0: { rubidium@10207: if (!IsTileOwner(tile, old_owner)) return; truelight@0: rubidium@10207: if (new_owner != INVALID_OWNER) { rubidium@10207: SetTileOwner(tile, new_owner); smatz@8520: return; truelight@0: } smatz@8520: smatz@8520: /* Remove depot */ smatz@8520: if (IsShipDepot(tile)) DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR); smatz@8520: smatz@8520: /* Set owner of canals and locks ... and also canal under dock there was before. smatz@8520: * Check if the new owner after removing depot isn't OWNER_WATER. */ rubidium@10207: if (IsTileOwner(tile, old_owner)) SetTileOwner(tile, OWNER_NONE); truelight@0: } truelight@0: rubidium@8119: static VehicleEnterTileStatus VehicleEnter_Water(Vehicle *v, TileIndex tile, int x, int y) truelight@0: { rubidium@5991: return VETSB_CONTINUE; truelight@0: } truelight@0: rubidium@7494: static CommandCost TerraformTile_Water(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new) rubidium@7494: { rubidium@7494: /* Canals can't be terraformed */ rubidium@7739: if (IsWaterTile(tile) && IsCanal(tile)) return_cmd_error(STR_MUST_DEMOLISH_CANAL_FIRST); rubidium@7494: rubidium@7494: return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@7494: } rubidium@7494: truelight@0: rubidium@5587: 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@7335: GetFoundation_Water, /* get_foundation_proc */ rubidium@7494: TerraformTile_Water, /* terraform_tile_proc */ truelight@0: };