tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file road_cmd.cpp Commands related to road tiles. */ belugas@6393: 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@3101: #include "rail_map.h" tron@3144: #include "road_map.h" rubidium@8102: #include "road_internal.h" tron@4232: #include "sprite.h" rubidium@8119: #include "tile_cmd.h" maedhros@6343: #include "landscape.h" tron@3319: #include "town_map.h" rubidium@8224: #include "viewport_func.h" rubidium@8116: #include "command_func.h" truelight@0: #include "town.h" KUDr@3900: #include "yapf/yapf.h" rubidium@8962: #include "depot_base.h" rubidium@8962: #include "depot_func.h" maedhros@6541: #include "newgrf.h" rubidium@6679: #include "station_map.h" rubidium@6679: #include "tunnel_map.h" rubidium@8211: #include "variables.h" rubidium@7582: #include "autoslope.h" belugas@7849: #include "transparency.h" smatz@8083: #include "tunnelbridge_map.h" rubidium@8106: #include "window_func.h" rubidium@8114: #include "strings_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8144: #include "vehicle_base.h" rubidium@8157: #include "sound_func.h" smatz@8342: #include "road_func.h" smatz@8398: #include "tunnelbridge.h" rubidium@8965: #include "cheat_func.h" rubidium@9006: #include "functions.h" rubidium@9009: #include "effectvehicle_func.h" smatz@9154: #include "elrail_func.h" rubidium@9038: #include "oldpool_func.h" smatz@8083: rubidium@8264: #include "table/sprites.h" rubidium@8264: #include "table/strings.h" rubidium@7582: rubidium@9298: rubidium@9298: bool RoadVehiclesAreBuilt() rubidium@9298: { rubidium@9298: const Vehicle* v; rubidium@9298: rubidium@9298: FOR_ALL_VEHICLES(v) { rubidium@9298: if (v->type == VEH_ROAD) return true; rubidium@9298: } rubidium@9298: return false; rubidium@9298: } rubidium@9298: rubidium@9298: /** rubidium@9298: * Change the side of the road vehicles drive on (server only). rubidium@9298: * @param tile unused rubidium@9298: * @param flags operation to perform rubidium@9298: * @param p1 the side of the road; 0 = left side and 1 = right side rubidium@9298: * @param p2 unused rubidium@9298: */ rubidium@9298: CommandCost CmdSetRoadDriveSide(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) rubidium@9298: { rubidium@9298: /* Check boundaries and you can only change this if NO vehicles have been built yet, rubidium@9298: * except in the intro-menu where of course it's always possible to do so. */ rubidium@9298: if (p1 > 1 || (_game_mode != GM_MENU && RoadVehiclesAreBuilt())) return CMD_ERROR; rubidium@9298: rubidium@9298: if (flags & DC_EXEC) { rubidium@9346: if (_game_mode == GM_MENU) { glx@9461: _settings_newgame.vehicle.road_side = p1; rubidium@9346: } else { rubidium@9413: _settings_game.vehicle.road_side = p1; rubidium@9346: } rubidium@9298: InvalidateWindow(WC_GAME_OPTIONS, 0); rubidium@9298: } rubidium@9298: return CommandCost(); rubidium@9298: } rubidium@9298: rubidium@7582: #define M(x) (1 << (x)) rubidium@7582: /* Level crossings may only be built on these slopes */ rubidium@7582: static const uint32 VALID_LEVEL_CROSSING_SLOPES = (M(SLOPE_SEN) | M(SLOPE_ENW) | M(SLOPE_NWS) | M(SLOPE_NS) | M(SLOPE_WSE) | M(SLOPE_EW) | M(SLOPE_FLAT)); rubidium@7582: #undef M truelight@0: skidd13@8744: /* Invalid RoadBits on slopes */ skidd13@8744: static const RoadBits _invalid_tileh_slopes_road[2][15] = { skidd13@8744: /* The inverse of the mixable RoadBits on a leveled slope */ skidd13@8744: { skidd13@8744: ROAD_NONE, // SLOPE_FLAT skidd13@8744: ROAD_NE | ROAD_SE, // SLOPE_W skidd13@8744: ROAD_NE | ROAD_NW, // SLOPE_S skidd13@8744: skidd13@8744: ROAD_NE, // SLOPE_SW skidd13@8744: ROAD_NW | ROAD_SW, // SLOPE_E skidd13@8744: ROAD_NONE, // SLOPE_EW skidd13@8744: skidd13@8744: ROAD_NW, // SLOPE_SE skidd13@8744: ROAD_NONE, // SLOPE_WSE skidd13@8744: ROAD_SE | ROAD_SW, // SLOPE_N skidd13@8744: skidd13@8744: ROAD_SE, // SLOPE_NW skidd13@8744: ROAD_NONE, // SLOPE_NS skidd13@8744: ROAD_NONE, // SLOPE_ENW skidd13@8744: skidd13@8744: ROAD_SW, // SLOPE_NE skidd13@8744: ROAD_NONE, // SLOPE_SEN skidd13@8744: ROAD_NONE // SLOPE_NWS skidd13@8744: }, skidd13@8744: /* The inverse of the allowed straight roads on a slope skidd13@8744: * (with and without a foundation). */ skidd13@8744: { skidd13@8744: ROAD_NONE, // SLOPE_FLAT skidd13@8744: ROAD_NONE, // SLOPE_W Foundation skidd13@8744: ROAD_NONE, // SLOPE_S Foundation skidd13@8744: skidd13@8744: ROAD_Y, // SLOPE_SW skidd13@8744: ROAD_NONE, // SLOPE_E Foundation skidd13@8744: ROAD_ALL, // SLOPE_EW skidd13@8744: skidd13@8744: ROAD_X, // SLOPE_SE skidd13@8744: ROAD_ALL, // SLOPE_WSE skidd13@8744: ROAD_NONE, // SLOPE_N Foundation skidd13@8744: skidd13@8744: ROAD_X, // SLOPE_NW skidd13@8744: ROAD_ALL, // SLOPE_NS skidd13@8744: ROAD_ALL, // SLOPE_ENW skidd13@8744: skidd13@8744: ROAD_Y, // SLOPE_NE skidd13@8744: ROAD_ALL, // SLOPE_SEN skidd13@8744: ROAD_ALL // SLOPE_NW skidd13@8744: } skidd13@8744: }; skidd13@8744: smatz@8384: Foundation GetRoadFoundation(Slope tileh, RoadBits bits); smatz@8384: frosch@9342: /** frosch@9342: * Is it allowed to remove the given road bits from the given tile? frosch@9342: * @param tile the tile to remove the road from frosch@9342: * @param remove the roadbits that are going to be removed frosch@9342: * @param owner the actual owner of the roadbits of the tile frosch@9342: * @param rt the road type to remove the bits from frosch@9342: * @param flags command flags frosch@9342: * @param town_check Shall the town rating checked/affected frosch@9342: * @return true when it is allowed to remove the road bits frosch@9342: */ frosch@9342: bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, RoadType rt, uint32 flags, bool town_check) truelight@0: { rubidium@6661: if (_game_mode == GM_EDITOR || remove == ROAD_NONE) return true; truelight@0: rubidium@6751: /* Water can always flood and towns can always remove "normal" road pieces. rubidium@6751: * Towns are not be allowed to remove non "normal" road pieces, like tram rubidium@6751: * tracks as that would result in trams that cannot turn. */ rubidium@6751: if (_current_player == OWNER_WATER || rubidium@9652: (rt == ROADTYPE_ROAD && !IsValidPlayerID(_current_player))) return true; truelight@0: belugas@6393: /* Only do the special processing if the road is owned belugas@6393: * by a town */ Darkvater@4849: if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner); truelight@0: frosch@9342: if (!town_check) return true; frosch@9342: tron@3017: if (_cheats.magic_bulldozer.value) return true; truelight@0: frosch@9342: Town *t = ClosestTownFromTile(tile, UINT_MAX); frosch@9342: if (t == NULL) return true; frosch@9342: frosch@9342: /* check if you're allowed to remove the street owned by a town frosch@9342: * removal allowance depends on difficulty setting */ frosch@9342: if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return false; frosch@9342: belugas@6393: /* Get a bitmask of which neighbouring roads has a tile */ skidd13@8734: RoadBits n = ROAD_NONE; skidd13@8734: RoadBits present = GetAnyRoadBits(tile, rt); rubidium@6661: if (present & ROAD_NE && GetAnyRoadBits(TILE_ADDXY(tile, -1, 0), rt) & ROAD_SW) n |= ROAD_NE; rubidium@6661: if (present & ROAD_SE && GetAnyRoadBits(TILE_ADDXY(tile, 0, 1), rt) & ROAD_NW) n |= ROAD_SE; rubidium@6661: if (present & ROAD_SW && GetAnyRoadBits(TILE_ADDXY(tile, 1, 0), rt) & ROAD_NE) n |= ROAD_SW; rubidium@6661: if (present & ROAD_NW && GetAnyRoadBits(TILE_ADDXY(tile, 0, -1), rt) & ROAD_SE) n |= ROAD_NW; truelight@201: frosch@9342: int rating_decrease = RATING_ROAD_DOWN_STEP_EDGE; belugas@6393: /* If 0 or 1 bits are set in n, or if no bits that match the bits to remove, belugas@6393: * then allow it */ skidd13@8734: if (KillFirstBit(n) != ROAD_NONE && (n & remove) != ROAD_NONE) { belugas@6393: /* you can remove all kind of roads with extra dynamite */ rubidium@9413: if (!_settings_game.construction.extra_dynamite) { frosch@9342: SetDParam(0, t->index); frosch@9342: _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; frosch@9342: return false; frosch@9342: } frosch@9342: rating_decrease = RATING_ROAD_DOWN_STEP_INNER; truelight@0: } frosch@9342: ChangeTownRating(t, rating_decrease, RATING_ROAD_MINIMUM); truelight@0: truelight@0: return true; truelight@0: } truelight@0: smatz@8428: Darkvater@1784: /** Delete a piece of road. tron@3491: * @param tile tile where to remove road from belugas@6483: * @param flags operation to perform smatz@8428: * @param pieces roadbits to remove smatz@8428: * @param rt roadtype to remove smatz@8428: * @param crossing_check should we check if there is a tram track when we are removing road from crossing? truelight@0: */ rubidium@8912: static CommandCost RemoveRoad(TileIndex tile, uint32 flags, RoadBits pieces, RoadType rt, bool crossing_check, bool town_check = true) truelight@0: { frosch@9341: RoadTypes rts = GetRoadTypes(tile); frosch@9341: /* The tile doesn't have the given road type */ frosch@9341: if (!HasBit(rts, rt)) return CMD_ERROR; frosch@9341: frosch@9342: bool town_road_under_stop = false; frosch@9342: rubidium@6679: switch (GetTileType(tile)) { rubidium@7370: case MP_ROAD: smatz@8075: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; rubidium@6679: break; rubidium@6679: rubidium@6679: case MP_STATION: rubidium@6679: if (!IsDriveThroughStopTile(tile)) return CMD_ERROR; frosch@9342: if (rt == ROADTYPE_ROAD) town_road_under_stop = GetStopBuiltOnTownRoad(tile); smatz@8075: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; rubidium@6679: break; rubidium@6679: rubidium@6679: case MP_TUNNELBRIDGE: smatz@8390: if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; smatz@8390: if (GetVehicleTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile)) != NULL) return CMD_ERROR; smatz@8390: break; rubidium@6679: rubidium@6679: default: rubidium@6679: return CMD_ERROR; rubidium@6679: } Darkvater@1638: frosch@9342: if (!CheckAllowRemoveRoad(tile, pieces, town_road_under_stop ? OWNER_TOWN : GetRoadOwner(tile, rt), rt, flags, town_check)) return CMD_ERROR; celestar@5385: rubidium@7370: if (!IsTileType(tile, MP_ROAD)) { rubidium@6679: /* If it's the last roadtype, just clear the whole tile */ rubidium@6679: if (rts == RoadTypeToRoadTypes(rt)) return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@6679: rubidium@8230: CommandCost cost(EXPENSES_CONSTRUCTION); rubidium@6679: if (IsTileType(tile, MP_TUNNELBRIDGE)) { smatz@8197: TileIndex other_end = GetOtherTunnelBridgeEnd(tile); rubidium@6679: /* Pay for *every* tile of the bridge or tunnel */ smatz@8398: cost.AddCost((GetTunnelBridgeLength(other_end, tile) + 2) * _price.remove_road); rubidium@6679: if (flags & DC_EXEC) { rubidium@6679: SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt)); rubidium@6679: SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt)); rubidium@6679: rubidium@6679: /* Mark tiles diry that have been repaved */ rubidium@6714: MarkTileDirtyByTile(tile); rubidium@6679: MarkTileDirtyByTile(other_end); rubidium@6679: if (IsBridge(tile)) { smatz@8083: TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile)); rubidium@6679: rubidium@6713: for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t); rubidium@6679: } rubidium@6679: } rubidium@6679: } else { rubidium@6950: cost.AddCost(_price.remove_road); rubidium@6679: if (flags & DC_EXEC) { rubidium@6679: SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt)); rubidium@6679: MarkTileDirtyByTile(tile); rubidium@6679: } rubidium@6679: } rubidium@8230: return cost; rubidium@6679: } rubidium@6679: celestar@5385: switch (GetRoadTileType(tile)) { celestar@5385: case ROAD_TILE_NORMAL: { skidd13@8744: const Slope tileh = GetTileSlope(tile, NULL); rubidium@6661: RoadBits present = GetRoadBits(tile, rt); skidd13@8744: const RoadBits other = GetOtherRoadBits(tile, rt); skidd13@8744: const Foundation f = GetRoadFoundation(tileh, present); celestar@5385: smatz@9067: if (HasRoadWorks(tile) && _current_player != OWNER_WATER) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS); celestar@5385: skidd13@8744: /* Autocomplete to a straight road skidd13@8744: * @li on steep slopes skidd13@8744: * @li if the bits of the other roadtypes result in another foundation skidd13@8744: * @li if build on slopes is disabled */ rubidium@8747: if (IsSteepSlope(tileh) || (IsStraightRoad(other) && rubidium@8747: (other & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) != ROAD_NONE) || rubidium@9413: (tileh != SLOPE_FLAT && !_settings_game.construction.build_on_slopes)) { skidd13@8734: pieces |= MirrorRoadBits(pieces); celestar@5385: } celestar@5385: belugas@6393: /* limit the bits to delete to the existing bits. */ skidd13@8734: pieces &= present; skidd13@8734: if (pieces == ROAD_NONE) return CMD_ERROR; celestar@5385: skidd13@8744: /* Now set present what it will be after the remove */ skidd13@8744: present ^= pieces; skidd13@8744: skidd13@8744: /* Check for invalid RoadBit combinations on slopes */ skidd13@8744: if (tileh != SLOPE_FLAT && present != ROAD_NONE && skidd13@8744: (present & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) == present) { skidd13@8744: return CMD_ERROR; skidd13@8744: } skidd13@8744: celestar@5385: if (flags & DC_EXEC) { smatz@9067: if (HasRoadWorks(tile)) { smatz@9067: /* flooding tile with road works, don't forget to remove the effect vehicle too */ smatz@9067: assert(_current_player == OWNER_WATER); smatz@9067: Vehicle *v; smatz@9067: FOR_ALL_VEHICLES(v) { smatz@9067: if (v->type == VEH_EFFECT && TileVirtXY(v->x_pos, v->y_pos) == tile) { smatz@9067: delete v; smatz@9067: } smatz@9067: } smatz@9067: } rubidium@6662: if (present == ROAD_NONE) { rubidium@6661: RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt)); rubidium@6661: if (rts == ROADTYPES_NONE) { rubidium@7536: /* Includes MarkTileDirtyByTile() */ rubidium@6661: DoClearSquare(tile); rubidium@6661: } else { rubidium@6662: SetRoadBits(tile, ROAD_NONE, rt); rubidium@6661: SetRoadTypes(tile, rts); rubidium@7536: MarkTileDirtyByTile(tile); rubidium@6661: } celestar@5385: } else { rubidium@6764: /* When bits are removed, you *always* end up with something that rubidium@6764: * is not a complete straight road tile. However, trams do not have rubidium@6764: * onewayness, so they cannot remove it either. */ rubidium@6764: if (rt != ROADTYPE_TRAM) SetDisallowedRoadDirections(tile, DRD_NONE); rubidium@6661: SetRoadBits(tile, present, rt); celestar@5385: MarkTileDirtyByTile(tile); celestar@5385: } celestar@5385: } skidd13@8744: skidd13@8744: /* If we change the foundation we have to pay for it. */ skidd13@8744: return CommandCost(EXPENSES_CONSTRUCTION, CountBits(pieces) * _price.remove_road + skidd13@8744: ((GetRoadFoundation(tileh, present) != f) ? _price.terraform : (Money)0)); celestar@5385: } celestar@5385: celestar@5385: case ROAD_TILE_CROSSING: { celestar@5385: if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) { tron@3176: return CMD_ERROR; truelight@0: } truelight@0: rubidium@6697: /* Don't allow road to be removed from the crossing when there is tram; rubidium@6697: * we can't draw the crossing without trambits ;) */ frosch@8563: if (rt == ROADTYPE_ROAD && HasTileRoadType(tile, ROADTYPE_TRAM) && (flags & DC_EXEC || crossing_check)) return CMD_ERROR; rubidium@6697: glx@8187: if (flags & DC_EXEC) { rubidium@6661: RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt)); rubidium@6661: if (rts == ROADTYPES_NONE) { rubidium@9789: TrackBits tracks = GetCrossingRailBits(tile); rubidium@9789: bool reserved = GetCrossingReservation(tile); rubidium@9789: MakeRailNormal(tile, GetTileOwner(tile), tracks, GetRailType(tile)); rubidium@9789: if (reserved) SetTrackReservation(tile, tracks); rubidium@6661: } else { rubidium@6661: SetRoadTypes(tile, rts); rubidium@6661: } tron@3060: MarkTileDirtyByTile(tile); rubidium@5587: YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile))); truelight@0: } rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_road * 2); celestar@5385: } tron@3977: celestar@5385: default: celestar@5385: case ROAD_TILE_DEPOT: celestar@5385: return CMD_ERROR; truelight@0: } truelight@0: } truelight@0: truelight@0: smatz@8428: /** Delete a piece of road. smatz@8428: * @param tile tile where to remove road from smatz@8428: * @param flags operation to perform smatz@8428: * @param p1 bit 0..3 road pieces to remove (RoadBits) smatz@8428: * bit 4..5 road type smatz@8428: * @param p2 unused smatz@8428: */ smatz@8428: CommandCost CmdRemoveRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) smatz@8428: { smatz@8428: RoadType rt = (RoadType)GB(p1, 4, 2); smatz@8428: if (!IsValidRoadType(rt)) return CMD_ERROR; smatz@8428: smatz@8428: RoadBits pieces = Extract(p1); smatz@8428: smatz@8428: return RemoveRoad(tile, flags, pieces, rt, true); smatz@8428: } smatz@8428: rubidium@7641: /** rubidium@7641: * Calculate the costs for roads on slopes rubidium@7641: * Aside modify the RoadBits to fit on the slopes rubidium@7641: * rubidium@7641: * @note The RoadBits are modified too! rubidium@7641: * @param tileh The current slope rubidium@7641: * @param pieces The RoadBits we want to add skidd13@8744: * @param existing The existent RoadBits of the current type skidd13@8744: * @param other The other existent RoadBits rubidium@7641: * @return The costs for these RoadBits on this slope rubidium@7641: */ smatz@8821: static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other) truelight@0: { skidd13@8744: /* Remove already build pieces */ skidd13@8744: CLRBITS(*pieces, existing); skidd13@8744: skidd13@8744: /* If we can't build anything stop here */ skidd13@8744: if (*pieces == ROAD_NONE) return CMD_ERROR; skidd13@8744: skidd13@8744: /* All RoadBit combos are valid on flat land */ skidd13@8744: if (tileh == SLOPE_FLAT) return CommandCost(); skidd13@8744: skidd13@8734: /* Proceed steep Slopes first to reduce lookup table size */ tron@4246: if (IsSteepSlope(tileh)) { rubidium@7641: /* Force straight roads. */ rubidium@7641: *pieces |= MirrorRoadBits(*pieces); rubidium@7332: skidd13@8744: /* Use existing as all existing since only straight skidd13@8744: * roads are allowed here. */ skidd13@8744: existing |= other; skidd13@8744: skidd13@8744: if ((existing == ROAD_NONE || existing == *pieces) && IsStraightRoad(*pieces)) { skidd13@8744: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); tron@4246: } tron@4246: return CMD_ERROR; tron@4246: } rubidium@7641: skidd13@8744: /* Save the merge of all bits of the current type */ skidd13@8744: RoadBits type_bits = existing | *pieces; rubidium@7641: skidd13@8744: /* Roads on slopes */ rubidium@9413: if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) { skidd13@8744: skidd13@8744: /* If we add leveling we've got to pay for it */ skidd13@8744: if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); skidd13@8744: skidd13@8744: return CommandCost(); rubidium@7641: } truelight@0: skidd13@8744: /* Autocomplete uphill roads */ skidd13@8744: *pieces |= MirrorRoadBits(*pieces); skidd13@8744: type_bits = existing | *pieces; tron@3381: skidd13@8744: /* Uphill roads */ skidd13@8744: if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) && skidd13@8744: (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) { tron@3381: skidd13@8744: /* Slopes with foundation ? */ skidd13@8744: if (IsSlopeWithOneCornerRaised(tileh)) { rubidium@7332: skidd13@8744: /* Prevent build on slopes if it isn't allowed */ rubidium@9413: if (_settings_game.construction.build_on_slopes) { skidd13@8744: skidd13@8744: /* If we add foundation we've got to pay for it */ skidd13@8744: if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); skidd13@8744: skidd13@8744: return CommandCost(); skidd13@8744: } skidd13@8744: } else { skidd13@8744: if (CountBits(existing) == 1) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); skidd13@8744: return CommandCost(); skidd13@8744: } truelight@0: } truelight@0: return CMD_ERROR; truelight@0: } truelight@0: Darkvater@1784: /** Build a piece of road. tron@3491: * @param tile tile where to build road belugas@6483: * @param flags operation to perform tron@6134: * @param p1 bit 0..3 road pieces to build (RoadBits) rubidium@6661: * bit 4..5 road type rubidium@6764: * bit 6..7 disallowed directions to toggle Darkvater@1784: * @param p2 the town that is building the road (0 if not applicable) truelight@0: */ rubidium@6943: CommandCost CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@8230: CommandCost cost(EXPENSES_CONSTRUCTION); smatz@8821: rubidium@5587: RoadBits existing = ROAD_NONE; skidd13@8744: RoadBits other_bits = ROAD_NONE; truelight@201: Darkvater@1784: /* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero Darkvater@1784: * if a non-player is building the road */ rubidium@9652: if ((IsValidPlayerID(_current_player) && p2 != 0) || (_current_player == OWNER_TOWN && !IsValidTownID(p2))) return CMD_ERROR; tron@6134: tron@6134: RoadBits pieces = Extract(p1); Darkvater@1784: smatz@8690: /* do not allow building 'zero' road bits, code wouldn't handle it */ smatz@8690: if (pieces == ROAD_NONE) return CMD_ERROR; smatz@8690: rubidium@6661: RoadType rt = (RoadType)GB(p1, 4, 2); truelight@7857: if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR; rubidium@6661: rubidium@6764: DisallowedRoadDirections toggle_drd = (DisallowedRoadDirections)GB(p1, 6, 2); rubidium@6764: skidd13@8734: Slope tileh = GetTileSlope(tile, NULL); truelight@0: celestar@3434: switch (GetTileType(tile)) { rubidium@7370: case MP_ROAD: rubidium@3793: switch (GetRoadTileType(tile)) { rubidium@6764: case ROAD_TILE_NORMAL: { tron@4046: if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS); rubidium@6756: skidd13@8744: other_bits = GetOtherRoadBits(tile, rt); frosch@8563: if (!HasTileRoadType(tile, rt)) break; tron@4046: rubidium@6661: existing = GetRoadBits(tile, rt); skidd13@8734: bool crossing = !IsStraightRoad(existing | pieces); rubidium@6768: if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) { rubidium@6764: /* Junctions cannot be one-way */ rubidium@6764: return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION); rubidium@6764: } tron@3150: if ((existing & pieces) == pieces) { rubidium@6764: /* We only want to set the (dis)allowed road directions */ frosch@9341: if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM && IsRoadOwner(tile, ROADTYPE_ROAD, _current_player)) { rubidium@6764: if (crossing) return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION); rubidium@6764: smatz@8601: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; smatz@8601: rubidium@6764: /* Ignore half built tiles */ skidd13@8734: if (flags & DC_EXEC && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) { rubidium@6764: SetDisallowedRoadDirections(tile, GetDisallowedRoadDirections(tile) ^ toggle_drd); rubidium@6764: MarkTileDirtyByTile(tile); rubidium@6764: } rubidium@6950: return CommandCost(); rubidium@6764: } tron@3066: return_cmd_error(STR_1007_ALREADY_BUILT); tron@3066: } rubidium@6764: } break; tron@3066: rubidium@3793: case ROAD_TILE_CROSSING: frosch@8563: if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT); skidd13@8744: other_bits = GetCrossingRoadBits(tile); skidd13@8744: if (pieces & ComplementRoadBits(other_bits)) goto do_clear; rubidium@6661: break; tron@3066: tron@3069: default: rubidium@3793: case ROAD_TILE_DEPOT: tron@3066: goto do_clear; tron@3060: } tron@3060: break; truelight@0: tron@3060: case MP_RAILWAY: { tron@3636: if (IsSteepSlope(tileh)) { tron@3060: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); tron@3060: } tron@3060: tron@3104: /* Level crossings may only be built on these slopes */ skidd13@7928: if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) { tron@3060: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); tron@3060: } tron@3060: rubidium@3792: if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear; smatz@8821: smatz@8821: Axis roaddir; tron@3269: switch (GetTrackBits(tile)) { tron@3269: case TRACK_BIT_X: tron@3269: if (pieces & ROAD_X) goto do_clear; tron@3269: roaddir = AXIS_Y; tron@3269: break; tron@3269: tron@3269: case TRACK_BIT_Y: tron@3269: if (pieces & ROAD_Y) goto do_clear; tron@3269: roaddir = AXIS_X; tron@3269: break; tron@3269: tron@3269: default: goto do_clear; tron@3060: } tron@3060: belugas@6406: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; tron@3381: tron@3060: if (flags & DC_EXEC) { rubidium@5587: YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile))); rubidium@6697: /* Always add road to the roadtypes (can't draw without it) */ rubidium@9789: bool reserved = HasBit(GetTrackReservation(tile), AxisToTrack(OtherAxis(roaddir))); rubidium@6697: MakeRoadCrossing(tile, _current_player, _current_player, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2); rubidium@9789: SetCrossingReservation(tile, reserved); smatz@8344: UpdateLevelCrossing(tile, false); tron@3100: MarkTileDirtyByTile(tile); tron@3060: } rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.build_road * (rt == ROADTYPE_ROAD ? 2 : 4)); tron@3017: } truelight@0: rubidium@6679: case MP_STATION: rubidium@9711: if (!IsRoadStop(tile)) goto do_clear; rubidium@9710: if (IsDriveThroughStopTile(tile)) { rubidium@9710: if (pieces & ~AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)))) goto do_clear; rubidium@9710: } else { rubidium@9710: if (pieces & ~DiagDirToRoadBits(GetRoadStopDir(tile))) goto do_clear; rubidium@9710: } frosch@8563: if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT); rubidium@6679: break; rubidium@6679: rubidium@6679: case MP_TUNNELBRIDGE: smatz@8390: if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; rubidium@9583: if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) return CMD_ERROR; frosch@8563: if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT); smatz@8390: /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */ smatz@8390: if (GetVehicleTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile)) != NULL) return CMD_ERROR; smatz@8390: break; rubidium@6679: smatz@8821: default: { truelight@0: do_clear:; smatz@8821: CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3381: if (CmdFailed(ret)) return ret; rubidium@6950: cost.AddCost(ret); smatz@8821: } break; truelight@0: } truelight@0: skidd13@8744: if (other_bits != pieces) { rubidium@6715: /* Check the foundation/slopes when adding road/tram bits */ smatz@8821: CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits); rubidium@6715: /* Return an error if we need to build a foundation (ret != 0) but the rubidium@6715: * current patch-setting is turned off (or stupid AI@work) */ rubidium@9413: if (CmdFailed(ret) || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) { rubidium@6715: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); rubidium@6715: } rubidium@6950: cost.AddCost(ret); rubidium@6715: } truelight@0: rubidium@7370: if (IsTileType(tile, MP_ROAD)) { belugas@6393: /* Don't put the pieces that already exist */ tron@3150: pieces &= ComplementRoadBits(existing); smatz@8384: smatz@8384: /* Check if new road bits will have the same foundation as other existing road types */ frosch@8563: if (IsNormalRoad(tile)) { smatz@8384: Slope slope = GetTileSlope(tile, NULL); smatz@8384: Foundation found_new = GetRoadFoundation(slope, pieces | existing); smatz@8384: smatz@8384: /* Test if all other roadtypes can be built at that foundation */ smatz@8384: for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) { smatz@8384: if (rtest != rt) { // check only other road types smatz@8384: RoadBits bits = GetRoadBits(tile, rtest); smatz@8384: /* do not check if there are not road bits of given type */ smatz@8384: if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) { smatz@8384: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); smatz@8384: } smatz@8384: } smatz@8384: } smatz@8384: } truelight@0: } truelight@0: smatz@8601: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; smatz@8601: truelight@7832: cost.AddCost(CountBits(pieces) * _price.build_road); rubidium@6679: if (IsTileType(tile, MP_TUNNELBRIDGE)) { rubidium@6679: /* Pay for *every* tile of the bridge or tunnel */ smatz@8398: cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2); rubidium@6679: } truelight@0: truelight@0: if (flags & DC_EXEC) { rubidium@6679: switch (GetTileType(tile)) { rubidium@7370: case MP_ROAD: { rubidium@6679: RoadTileType rtt = GetRoadTileType(tile); rubidium@6679: if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) { rubidium@6679: SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt)); rubidium@6679: SetRoadOwner(tile, rt, _current_player); rubidium@6968: if (_current_player == OWNER_TOWN && rt == ROADTYPE_ROAD) SetTownIndex(tile, p2); rubidium@6679: } rubidium@6679: if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt); rubidium@6679: } break; rubidium@6679: rubidium@6679: case MP_TUNNELBRIDGE: { smatz@8197: TileIndex other_end = GetOtherTunnelBridgeEnd(tile); rubidium@6679: rubidium@6679: SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt)); rubidium@6661: SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt)); rubidium@6679: rubidium@6679: /* Mark tiles diry that have been repaved */ rubidium@6679: MarkTileDirtyByTile(other_end); rubidium@6714: MarkTileDirtyByTile(tile); rubidium@6679: if (IsBridge(tile)) { smatz@8083: TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile)); rubidium@6679: rubidium@6713: for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t); rubidium@6679: } rubidium@6679: } break; rubidium@6679: rubidium@6679: case MP_STATION: rubidium@6679: SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt)); frosch@9342: if (IsDriveThroughStopTile(tile) && rt == ROADTYPE_ROAD) SetStopBuiltOnTownRoad(tile, false); rubidium@6679: break; rubidium@6679: rubidium@6679: default: rubidium@6679: MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_player, _current_player, _current_player); rubidium@6679: break; truelight@0: } truelight@0: frosch@8563: if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) { rubidium@6764: existing |= pieces; skidd13@8734: SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ? rubidium@6764: GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE); rubidium@6764: } rubidium@6764: truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: return cost; truelight@0: } truelight@0: Darkvater@1784: /** Build a long piece of road. tron@3491: * @param end_tile end tile of drag belugas@6483: * @param flags operation to perform Darkvater@1784: * @param p1 start tile of drag Darkvater@1784: * @param p2 various bitstuffed elements Darkvater@1784: * - p2 = (bit 0) - start tile starts in the 2nd half of tile (p2 & 1) Darkvater@1784: * - p2 = (bit 1) - end tile starts in the 2nd half of tile (p2 & 2) Darkvater@1784: * - p2 = (bit 2) - direction: 0 = along x-axis, 1 = along y-axis (p2 & 4) rubidium@6662: * - p2 = (bit 3 + 4) - road type rubidium@6764: * - p2 = (bit 5) - set road direction Darkvater@1784: */ rubidium@6943: CommandCost CmdBuildLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { smatz@8821: CommandCost cost(EXPENSES_CONSTRUCTION); rubidium@6710: bool had_bridge = false; rubidium@7428: bool had_tunnel = false; rubidium@6764: bool had_success = false; rubidium@6764: DisallowedRoadDirections drd = DRD_NORTHBOUND; truelight@0: smatz@8601: _error_message = INVALID_STRING_ID; smatz@8601: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1784: smatz@8821: TileIndex start_tile = p1; rubidium@6662: RoadType rt = (RoadType)GB(p2, 3, 2); truelight@7857: if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR; truelight@0: Darkvater@1784: /* Only drag in X or Y direction dictated by the direction variable */ skidd13@7928: if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis skidd13@7928: if (HasBit(p2, 2) && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis Darkvater@1784: Darkvater@1784: /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */ skidd13@7928: if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) { Darkvater@1784: TileIndex t = start_tile; Darkvater@1784: start_tile = end_tile; Darkvater@1784: end_tile = t; skidd13@7954: p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0; rubidium@6764: drd = DRD_SOUTHBOUND; truelight@0: } truelight@0: rubidium@6764: /* On the X-axis, we have to swap the initial bits, so they rubidium@6764: * will be interpreted correctly in the GTTS. Futhermore rubidium@6764: * when you just 'click' on one tile to build them. */ skidd13@7928: if (HasBit(p2, 2) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH; rubidium@6764: /* No disallowed direction bits have to be toggled */ skidd13@7928: if (!HasBit(p2, 5)) drd = DRD_NONE; rubidium@6764: smatz@8821: TileIndex tile = start_tile; belugas@6393: /* Start tile is the small number. */ Darkvater@1784: for (;;) { skidd13@7928: RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X; tron@3098: skidd13@7928: if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE; skidd13@7928: if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW; truelight@0: rubidium@9583: _error_message = INVALID_STRING_ID; smatz@8821: CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD); Darkvater@1784: if (CmdFailed(ret)) { Darkvater@1784: if (_error_message != STR_1007_ALREADY_BUILT) return CMD_ERROR; tron@2549: } else { rubidium@6764: had_success = true; rubidium@7428: /* Only pay for the upgrade on one side of the bridges and tunnels */ rubidium@7428: if (IsTileType(tile, MP_TUNNELBRIDGE)) { rubidium@7428: if (IsBridge(tile)) { smatz@8083: if ((!had_bridge || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) { rubidium@7428: cost.AddCost(ret); rubidium@7428: } rubidium@7428: had_bridge = true; smatz@8390: } else { // IsTunnel(tile) smatz@8083: if ((!had_tunnel || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) { rubidium@7428: cost.AddCost(ret); rubidium@7428: } rubidium@7428: had_tunnel = true; rubidium@6710: } rubidium@6710: } else { rubidium@6950: cost.AddCost(ret); rubidium@6710: } tron@2549: } truelight@0: Darkvater@1784: if (tile == end_tile) break; truelight@0: skidd13@7928: tile += HasBit(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0); truelight@0: } truelight@0: rubidium@6764: return !had_success ? CMD_ERROR : cost; truelight@0: } truelight@0: Darkvater@1784: /** Remove a long piece of road. tron@3491: * @param end_tile end tile of drag belugas@6483: * @param flags operation to perform Darkvater@1784: * @param p1 start tile of drag Darkvater@1784: * @param p2 various bitstuffed elements Darkvater@1784: * - p2 = (bit 0) - start tile starts in the 2nd half of tile (p2 & 1) Darkvater@1784: * - p2 = (bit 1) - end tile starts in the 2nd half of tile (p2 & 2) Darkvater@1784: * - p2 = (bit 2) - direction: 0 = along x-axis, 1 = along y-axis (p2 & 4) rubidium@6662: * - p2 = (bit 3 + 4) - road type Darkvater@1784: */ rubidium@6943: CommandCost CmdRemoveLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { smatz@8821: CommandCost cost(EXPENSES_CONSTRUCTION); darkvater@889: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1784: smatz@8821: TileIndex start_tile = p1; rubidium@6662: RoadType rt = (RoadType)GB(p2, 3, 2); rubidium@6662: if (!IsValidRoadType(rt)) return CMD_ERROR; truelight@0: Darkvater@1784: /* Only drag in X or Y direction dictated by the direction variable */ skidd13@7928: if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis skidd13@7928: if (HasBit(p2, 2) && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis Darkvater@1784: Darkvater@1784: /* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */ skidd13@7928: if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) { Darkvater@1784: TileIndex t = start_tile; Darkvater@1784: start_tile = end_tile; Darkvater@1784: end_tile = t; skidd13@7954: p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0; truelight@0: } truelight@0: smatz@8821: Money money = GetAvailableMoneyForCommand(); smatz@8821: TileIndex tile = start_tile; belugas@6393: /* Start tile is the small number. */ Darkvater@1784: for (;;) { skidd13@7928: RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X; tron@3098: skidd13@7928: if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE; skidd13@7928: if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW; truelight@201: belugas@6393: /* try to remove the halves. */ tron@3098: if (bits != 0) { smatz@8821: CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true); rubidium@7442: if (CmdSucceeded(ret)) { rubidium@7442: if (flags & DC_EXEC) { rubidium@8230: money -= ret.GetCost(); rubidium@8230: if (money < 0) { rubidium@7442: _additional_cash_required = DoCommand(end_tile, start_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost(); rubidium@7442: return cost; rubidium@7442: } rubidium@8912: RemoveRoad(tile, flags, bits, rt, true, false); rubidium@7442: } rubidium@7442: cost.AddCost(ret); rubidium@7442: } truelight@0: } truelight@0: Darkvater@1784: if (tile == end_tile) break; truelight@0: skidd13@7928: tile += HasBit(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0); truelight@0: } truelight@0: rubidium@6950: return (cost.GetCost() == 0) ? CMD_ERROR : cost; truelight@0: } truelight@0: Darkvater@1784: /** Build a road depot. tron@3491: * @param tile tile where to build the depot belugas@6483: * @param flags operation to perform tron@6134: * @param p1 bit 0..1 entrance direction (DiagDirection) rubidium@6662: * bit 2..3 road type Darkvater@1784: * @param p2 unused celestar@2085: * celestar@2085: * @todo When checking for the tile slope, celestar@2085: * distingush between "Flat land required" and "land sloped in wrong direction" truelight@0: */ rubidium@6943: CommandCost CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@6134: DiagDirection dir = Extract(p1); rubidium@6662: RoadType rt = (RoadType)GB(p1, 2, 2); rubidium@6662: truelight@7857: if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR; Darkvater@1784: smatz@8821: Slope tileh = GetTileSlope(tile, NULL); tron@3636: if (tileh != SLOPE_FLAT && ( rubidium@9413: !_settings_game.construction.build_on_slopes || tron@3636: IsSteepSlope(tileh) || tron@6134: !CanBuildDepotByTileh(dir, tileh) tron@2549: )) { tron@2549: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: smatz@8821: CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); Darkvater@1784: if (CmdFailed(cost)) return CMD_ERROR; truelight@0: celestar@5385: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5385: rubidium@9036: if (!Depot::CanAllocateItem()) return CMD_ERROR; truelight@0: truelight@201: if (flags & DC_EXEC) { rubidium@9036: Depot *dep = new Depot(tile); truelight@0: dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index; truelight@0: rubidium@6661: MakeRoadDepot(tile, _current_player, dir, rt); tron@3100: MarkTileDirtyByTile(tile); truelight@0: } rubidium@6950: return cost.AddCost(_price.build_road_depot); truelight@0: } truelight@0: rubidium@6943: static CommandCost RemoveRoadDepot(TileIndex tile, uint32 flags) truelight@0: { smatz@8821: if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER) return CMD_ERROR; truelight@0: rubidium@7758: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; truelight@0: rubidium@7389: if (flags & DC_EXEC) { rubidium@7389: DoClearSquare(tile); rubidium@7389: delete GetDepotByTile(tile); rubidium@7389: } truelight@0: rubidium@8230: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_road_depot); truelight@0: } truelight@0: rubidium@6943: static CommandCost ClearTile_Road(TileIndex tile, byte flags) tron@1977: { rubidium@3793: switch (GetRoadTileType(tile)) { rubidium@3793: case ROAD_TILE_NORMAL: { rubidium@6661: RoadBits b = GetAllRoadBits(tile); truelight@0: Darkvater@5637: /* Clear the road if only one piece is on the tile OR the AI tries Darkvater@5637: * to clear town road OR we are not using the DC_AUTO flag */ truelight@7832: if ((CountBits(b) == 1 && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || frosch@9341: ((flags & DC_AI_BUILDING) && GetOtherRoadBits(tile, ROADTYPE_ROAD) == ROAD_NONE && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) || Darkvater@5637: !(flags & DC_AUTO) Darkvater@5637: ) { rubidium@6679: RoadTypes rts = GetRoadTypes(tile); rubidium@8230: CommandCost ret(EXPENSES_CONSTRUCTION); rubidium@6679: for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) { skidd13@7928: if (HasBit(rts, rt)) { smatz@8428: CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true); rubidium@6679: if (CmdFailed(tmp_ret)) return tmp_ret; rubidium@6950: ret.AddCost(tmp_ret); rubidium@6679: } rubidium@6679: } rubidium@6679: return ret; tron@3060: } Darkvater@5637: return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST); Darkvater@5643: } truelight@0: rubidium@3793: case ROAD_TILE_CROSSING: { rubidium@6814: RoadTypes rts = GetRoadTypes(tile); rubidium@8230: CommandCost ret(EXPENSES_CONSTRUCTION); tron@3060: tron@3069: if (flags & DC_AUTO) return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST); tron@3060: rubidium@6814: /* Must iterate over the roadtypes in a reverse manner because rubidium@6814: * tram tracks must be removed before the road bits. */ rubidium@8859: RoadType rt = ROADTYPE_HWAY; rubidium@8859: do { skidd13@7928: if (HasBit(rts, rt)) { smatz@8428: CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false); rubidium@6814: if (CmdFailed(tmp_ret)) return tmp_ret; rubidium@6950: ret.AddCost(tmp_ret); rubidium@6814: } rubidium@8859: } while (rt-- != ROADTYPE_ROAD); tron@3060: tron@3060: if (flags & DC_EXEC) { tron@3491: DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3060: } tron@3060: return ret; tron@3060: } tron@3060: tron@3069: default: rubidium@3793: case ROAD_TILE_DEPOT: tron@3060: if (flags & DC_AUTO) { tron@3060: return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); tron@3060: } tron@3060: return RemoveRoadDepot(tile, flags); truelight@0: } truelight@0: } truelight@0: truelight@0: rubidium@6248: struct DrawRoadTileStruct { truelight@0: uint16 image; truelight@0: byte subcoord_x; truelight@0: byte subcoord_y; rubidium@6248: }; truelight@0: truelight@0: #include "table/road_land.h" truelight@0: skidd13@8734: /** skidd13@8734: * Get the foundationtype of a RoadBits Slope combination skidd13@8734: * skidd13@8734: * @param tileh The Slope part skidd13@8734: * @param bits The RoadBits part skidd13@8734: * @return The resulting Foundation skidd13@8734: */ rubidium@7335: Foundation GetRoadFoundation(Slope tileh, RoadBits bits) tron@2548: { skidd13@8734: /* Flat land and land without a road doesn't require a foundation */ skidd13@8734: if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE; skidd13@8734: rubidium@7335: if (!IsSteepSlope(tileh)) { skidd13@8744: /* Leveled RoadBits on a slope */ skidd13@8744: if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED; skidd13@8744: skidd13@8744: /* Straight roads without foundation on a slope */ skidd13@8744: if (!IsSlopeWithOneCornerRaised(tileh) && skidd13@8744: (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE) skidd13@8744: return FOUNDATION_NONE; tron@4246: } truelight@0: skidd13@8744: /* Roads on steep Slopes or on Slopes with one corner raised */ rubidium@7335: return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y); truelight@0: } truelight@0: truelight@0: const byte _road_sloped_sprites[14] = { truelight@0: 0, 0, 2, 0, truelight@0: 0, 1, 0, 0, truelight@0: 3, 0, 0, 0, truelight@0: 0, 0 truelight@0: }; truelight@0: peter1138@2471: /** maedhros@6541: * Whether to draw unpaved roads regardless of the town zone. maedhros@6541: * By default, OpenTTD always draws roads as unpaved if they are on a desert maedhros@6541: * tile or above the snowline. Newgrf files, however, can set a bit that allows maedhros@6541: * paved roads to be built on desert tiles as they would be on grassy tiles. maedhros@6541: * maedhros@6541: * @param tile The tile the road is on maedhros@6541: * @param roadside What sort of road this is maedhros@6541: * @return True if the road should be drawn unpaved regardless of the roadside. maedhros@6541: */ maedhros@6541: static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside) maedhros@6541: { maedhros@6541: return (IsOnSnow(tile) && rubidium@9413: !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) && maedhros@6541: roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS)); maedhros@6541: } maedhros@6541: maedhros@6541: /** rubidium@6691: * Draws the catenary for the given tile rubidium@6691: * @param ti information about the tile (slopes, height etc) rubidium@6691: * @param tram the roadbits for the tram rubidium@6691: */ smatz@8821: void DrawTramCatenary(const TileInfo *ti, RoadBits tram) rubidium@6691: { smatz@8806: /* Do not draw catenary if it is invisible */ smatz@8806: if (IsInvisibilitySet(TO_CATENARY)) return; smatz@8806: rubidium@6691: /* Don't draw the catenary under a low bridge */ smatz@8528: if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) { rubidium@6691: uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile)); rubidium@6691: rubidium@6699: if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return; rubidium@6691: } rubidium@6691: rubidium@6691: SpriteID front; rubidium@6691: SpriteID back; rubidium@6691: rubidium@6691: if (ti->tileh != SLOPE_FLAT) { rubidium@6691: back = SPR_TRAMWAY_BACK_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1]; rubidium@6691: front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1]; rubidium@6691: } else { rubidium@6691: back = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram]; rubidium@6691: front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram]; rubidium@6691: } rubidium@6691: smatz@8528: AddSortableSpriteToDraw(back, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY)); smatz@8528: AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY)); rubidium@6691: } rubidium@6691: rubidium@6691: /** rubidium@6764: * Draws details on/around the road rubidium@6764: * @param img the sprite to draw rubidium@6764: * @param ti the tile to draw on rubidium@6764: * @param dx the offset from the top of the BB of the tile rubidium@6764: * @param dy the offset from the top of the BB of the tile rubidium@6764: * @param h the height of the sprite to draw rubidium@6764: */ smatz@8821: static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h) rubidium@6764: { rubidium@6764: int x = ti->x | dx; rubidium@6764: int y = ti->y | dy; rubidium@6764: byte z = ti->z; rubidium@6764: if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y); rubidium@6764: AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z); rubidium@6764: } rubidium@6764: rubidium@6764: /** peter1138@2471: * Draw ground sprite and road pieces peter1138@2471: * @param ti TileInfo peter1138@2471: */ smatz@8821: static void DrawRoadBits(TileInfo *ti) peter1138@2471: { rubidium@6661: RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD); rubidium@6691: RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM); rubidium@6691: peter1138@5668: SpriteID image = 0; peter1138@5668: SpriteID pal = PAL_NONE; peter1138@2471: tron@3636: if (ti->tileh != SLOPE_FLAT) { rubidium@7335: DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram)); peter1138@2471: belugas@6393: /* DrawFoundation() modifies ti. belugas@6393: * Default sloped sprites.. */ tron@3636: if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F; peter1138@2471: } peter1138@2471: rubidium@6691: if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram]; peter1138@2471: smatz@8821: Roadside roadside = GetRoadside(ti->tile); peter1138@2471: maedhros@6541: if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) { peter1138@2471: image += 19; tron@4048: } else { tron@4048: switch (roadside) { peter1138@5668: case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break; tron@4048: case ROADSIDE_GRASS: break; tron@4048: case ROADSIDE_GRASS_ROAD_WORKS: break; tron@4048: default: image -= 19; break; // Paved tron@4048: } peter1138@2471: } peter1138@2471: peter1138@5668: DrawGroundSprite(image, pal); peter1138@2471: rubidium@6691: /* For tram we overlay the road graphics with either tram tracks only rubidium@6691: * (when there is actual road beneath the trams) or with tram tracks rubidium@6691: * and some dirts which hides the road graphics */ rubidium@6691: if (tram != ROAD_NONE) { rubidium@6691: if (ti->tileh != SLOPE_FLAT) { rubidium@6691: image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET; rubidium@6691: } else { rubidium@6691: image = _road_tile_sprites_1[tram] - SPR_ROAD_Y; rubidium@6691: } rubidium@6691: image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY; rubidium@6691: DrawGroundSprite(image, pal); rubidium@6691: } rubidium@6691: rubidium@6965: if (road != ROAD_NONE) { rubidium@6965: DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile); rubidium@6965: if (drd != DRD_NONE) { rubidium@6965: DrawRoadDetail(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), ti, 8, 8, 0); rubidium@6965: } rubidium@6965: } rubidium@6965: celestar@3430: if (HasRoadWorks(ti->tile)) { belugas@6393: /* Road works */ rubidium@6691: DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE); peter1138@2471: return; peter1138@2471: } peter1138@2471: rubidium@6691: if (tram != ROAD_NONE) DrawTramCatenary(ti, tram); rubidium@6691: belugas@6393: /* Return if full detail is disabled, or we are zoomed fully out. */ skidd13@7928: if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return; tron@5148: smatz@8589: /* Do not draw details (street lights, trees) under low bridge */ smatz@8589: if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) { smatz@8589: uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile)); smatz@8589: uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT; smatz@8589: smatz@8589: if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT; smatz@8589: smatz@8589: if (height < minz) return; smatz@8589: } smatz@8589: belugas@8647: /* If there are no road bits, return, as there is nothing left to do */ belugas@8647: if (CountBits(road) < 2) return; belugas@8647: belugas@6393: /* Draw extra details. */ smatz@8821: for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) { rubidium@6764: DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10); peter1138@2471: } peter1138@2471: } peter1138@2471: truelight@0: static void DrawTile_Road(TileInfo *ti) truelight@0: { rubidium@3793: switch (GetRoadTileType(ti->tile)) { rubidium@3793: case ROAD_TILE_NORMAL: tron@4081: DrawRoadBits(ti); tron@3060: break; truelight@0: rubidium@3793: case ROAD_TILE_CROSSING: { rubidium@7335: if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED); truelight@0: smatz@8821: SpriteID image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.crossing; smatz@8821: SpriteID pal = PAL_NONE; tron@3060: tron@3272: if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++; celestar@3322: if (IsCrossingBarred(ti->tile)) image += 2; tron@3060: smatz@8821: Roadside roadside = GetRoadside(ti->tile); smatz@8821: maedhros@6541: if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) { tron@3060: image += 8; tron@3060: } else { maedhros@6541: switch (roadside) { peter1138@5668: case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break; tron@4048: case ROADSIDE_GRASS: break; tron@4048: default: image += 4; break; // Paved tron@4048: } tron@3060: } tron@3060: peter1138@5668: DrawGroundSprite(image, pal); rubidium@9785: rubidium@9785: /* PBS debugging, draw reserved tracks darker */ rubidium@9785: if (_settings_client.gui.show_track_reservation && GetCrossingReservation(ti->tile)) { rubidium@9785: DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x, PALETTE_CRASH); rubidium@9785: } rubidium@9785: frosch@8563: if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) { rubidium@6697: DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal); rubidium@6697: DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile)); rubidium@6697: } smatz@9154: if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti); tron@3060: break; truelight@0: } truelight@0: tron@3098: default: rubidium@3793: case ROAD_TILE_DEPOT: { rubidium@7335: if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED); tron@1398: smatz@8821: SpriteID palette = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)); truelight@0: smatz@8821: const DrawTileSprites *dts; frosch@8563: if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) { rubidium@6691: dts = &_tram_depot[GetRoadDepotDirection(ti->tile)]; rubidium@6691: } else { rubidium@6691: dts = &_road_depot[GetRoadDepotDirection(ti->tile)]; rubidium@6691: } rubidium@6691: frosch@8571: DrawGroundSprite(dts->ground.sprite, PAL_NONE); tron@3060: smatz@8806: /* End now if buildings are invisible */ smatz@8806: if (IsInvisibilitySet(TO_BUILDINGS)) break; smatz@8806: smatz@8821: for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) { frosch@8570: SpriteID image = dtss->image.sprite; peter1138@5668: SpriteID pal; tron@3060: skidd13@7928: if (!IsTransparencySet(TO_BUILDINGS) && HasBit(image, PALETTE_MODIFIER_COLOR)) { peter1138@5668: pal = palette; peter1138@5668: } else { peter1138@5668: pal = PAL_NONE; tron@4226: } tron@3060: tron@4077: AddSortableSpriteToDraw( peter1138@5668: image, pal, tron@4232: ti->x + dtss->delta_x, ti->y + dtss->delta_y, tron@4232: dtss->size_x, dtss->size_y, rubidium@7333: dtss->size_z, ti->z, belugas@7849: IsTransparencySet(TO_BUILDINGS) tron@3060: ); tron@3060: } tron@3060: break; truelight@0: } truelight@0: } celestar@5385: DrawBridgeMiddle(ti); truelight@0: } truelight@0: rubidium@6666: void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt) truelight@0: { peter1138@5668: SpriteID palette = PLAYER_SPRITE_COLOR(_local_player); smatz@8821: const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir]; truelight@0: tron@2639: x += 33; tron@2639: y += 17; truelight@0: frosch@8571: DrawSprite(dts->ground.sprite, PAL_NONE, x, y); truelight@0: smatz@8821: for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) { tron@4232: Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z); frosch@8570: SpriteID image = dtss->image.sprite; truelight@201: skidd13@7928: DrawSprite(image, HasBit(image, PALETTE_MODIFIER_COLOR) ? palette : PAL_NONE, x + pt.x, y + pt.y); truelight@0: } truelight@0: } truelight@0: tron@4231: static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y) truelight@0: { tron@4231: uint z; tron@4231: Slope tileh = GetTileSlope(tile, &z); tron@3098: tron@3636: if (tileh == SLOPE_FLAT) return z; frosch@8563: if (IsNormalRoad(tile)) { rubidium@7335: Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile)); rubidium@7335: z += ApplyFoundationToSlope(f, &tileh); tron@4231: return z + GetPartialZ(x & 0xF, y & 0xF, tileh); tron@3286: } else { tron@3645: return z + TILE_HEIGHT; truelight@0: } dominik@39: } dominik@39: rubidium@7335: static Foundation GetFoundation_Road(TileIndex tile, Slope tileh) dominik@39: { frosch@8563: if (IsNormalRoad(tile)) { rubidium@7335: return GetRoadFoundation(tileh, GetAllRoadBits(tile)); tron@3286: } else { rubidium@7335: return FlatteningFoundation(tileh); dominik@39: } truelight@0: } truelight@0: tron@1977: static void GetAcceptedCargo_Road(TileIndex tile, AcceptedCargo ac) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: tron@1977: static void AnimateTile_Road(TileIndex tile) truelight@0: { tron@2639: if (IsLevelCrossing(tile)) MarkTileDirtyByTile(tile); truelight@0: } truelight@0: tron@4048: tron@4048: static const Roadside _town_road_types[][2] = { tron@4048: { ROADSIDE_GRASS, ROADSIDE_GRASS }, tron@4048: { ROADSIDE_PAVED, ROADSIDE_PAVED }, tron@4048: { ROADSIDE_PAVED, ROADSIDE_PAVED }, tron@4048: { ROADSIDE_TREES, ROADSIDE_TREES }, tron@4048: { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED } truelight@0: }; truelight@0: tron@4048: static const Roadside _town_road_types_2[][2] = { tron@4048: { ROADSIDE_GRASS, ROADSIDE_GRASS }, tron@4048: { ROADSIDE_PAVED, ROADSIDE_PAVED }, tron@4048: { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }, tron@4048: { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }, tron@4048: { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED } truelight@0: }; truelight@0: truelight@0: tron@1977: static void TileLoop_Road(TileIndex tile) truelight@0: { rubidium@9413: switch (_settings_game.game_creation.landscape) { belugas@6357: case LT_ARCTIC: maedhros@6343: if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) { celestar@3430: ToggleSnow(tile); tron@3017: MarkTileDirtyByTile(tile); tron@3017: } tron@3017: break; tron@3017: belugas@6357: case LT_TROPIC: celestar@3430: if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) { celestar@3430: ToggleDesert(tile); tron@3017: MarkTileDirtyByTile(tile); tron@3017: } tron@3017: break; truelight@0: } truelight@0: frosch@8563: if (IsRoadDepot(tile)) return; truelight@0: smatz@8821: const Town *t = ClosestTownFromTile(tile, (uint)-1); celestar@3430: if (!HasRoadWorks(tile)) { belugas@8308: HouseZonesBits grp = HZB_TOWN_EDGE; truelight@1280: truelight@0: if (t != NULL) { truelight@0: grp = GetTownRadiusGroup(t, tile); truelight@201: belugas@6393: /* Show an animation to indicate road work */ truelight@201: if (t->road_build_months != 0 && belugas@8308: (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) && frosch@8563: IsNormalRoad(tile) && CountBits(GetAllRoadBits(tile)) > 1 ) { skidd13@7967: if (GetTileSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) { celestar@3430: StartRoadWorks(tile); truelight@201: tron@541: SndPlayTileFx(SND_21_JACKHAMMER, tile); truelight@0: CreateEffectVehicleAbove( celestar@3421: TileX(tile) * TILE_SIZE + 7, celestar@3421: TileY(tile) * TILE_SIZE + 7, truelight@0: 0, tron@1359: EV_BULLDOZER); truelight@0: MarkTileDirtyByTile(tile); truelight@0: return; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: { celestar@3430: /* Adjust road ground type depending on 'grp' (grp is the distance to the center) */ rubidium@9413: const Roadside* new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp]; tron@4048: Roadside cur_rs = GetRoadside(tile); truelight@201: celestar@3430: /* We have our desired type, do nothing */ tron@4048: if (cur_rs == new_rs[0]) return; celestar@3430: celestar@3430: /* We have the pre-type of the desired type, switch to the desired type */ tron@4048: if (cur_rs == new_rs[1]) { tron@4048: cur_rs = new_rs[0]; celestar@3430: /* We have barren land, install the pre-type */ tron@4048: } else if (cur_rs == ROADSIDE_BARREN) { tron@4048: cur_rs = new_rs[1]; celestar@3430: /* We're totally off limits, remove any installation and make barren land */ truelight@0: } else { tron@4048: cur_rs = ROADSIDE_BARREN; truelight@0: } tron@4048: SetRoadside(tile, cur_rs); truelight@0: MarkTileDirtyByTile(tile); truelight@0: } celestar@3430: } else if (IncreaseRoadWorksCounter(tile)) { celestar@3430: TerminateRoadWorks(tile); rubidium@7641: rubidium@9413: if (_settings_game.economy.mod_road_rebuild) { rubidium@7641: /* Generate a nicer town surface */ rubidium@7641: const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD); rubidium@7641: const RoadBits new_rb = CleanUpRoadBits(tile, old_rb); rubidium@7641: rubidium@7641: if (old_rb != new_rb) { smatz@8428: RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true); rubidium@7641: } rubidium@7641: } rubidium@7641: truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: tron@1977: static void ClickTile_Road(TileIndex tile) truelight@0: { frosch@8563: if (IsRoadDepot(tile)) ShowDepotWindow(tile, VEH_ROAD); truelight@0: } truelight@0: frosch@8616: /* Converts RoadBits to TrackBits */ truelight@0: static const byte _road_trackbits[16] = { truelight@0: 0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F, truelight@0: }; truelight@0: frosch@8616: static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side) tron@1977: { frosch@8616: TrackdirBits trackdirbits = TRACKDIR_BIT_NONE; frosch@8616: TrackdirBits red_signals = TRACKDIR_BIT_NONE; // crossing barred tron@3066: switch (mode) { tron@3066: case TRANSPORT_RAIL: frosch@8616: if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile)); frosch@8616: break; truelight@201: tron@3066: case TRANSPORT_ROAD: frosch@8616: if ((GetRoadTypes(tile) & sub_mode) == 0) break; rubidium@3793: switch (GetRoadTileType(tile)) { rubidium@6683: case ROAD_TILE_NORMAL: { rubidium@6764: const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 }; smatz@8596: RoadType rt = (RoadType)FindFirstBit(sub_mode); smatz@8596: RoadBits bits = GetRoadBits(tile, rt); smatz@8596: smatz@8596: /* no roadbit at this side of tile, return 0 */ frosch@8616: if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break; smatz@8596: rubidium@6764: uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)]; frosch@8616: if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier); frosch@8616: break; rubidium@6683: } tron@3066: rubidium@3793: case ROAD_TILE_CROSSING: { smatz@8596: Axis axis = GetCrossingRoadAxis(tile); tron@3272: frosch@8616: if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break; smatz@8596: frosch@8616: trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis)); frosch@8616: if (IsCrossingBarred(tile)) red_signals = trackdirbits; frosch@8616: break; tron@3066: } tron@3066: tron@3069: default: smatz@8596: case ROAD_TILE_DEPOT: { smatz@8596: DiagDirection dir = GetRoadDepotDirection(tile); smatz@8596: frosch@8616: if (side != INVALID_DIAGDIR && side != dir) break; smatz@8596: smatz@9224: trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)); frosch@8616: break; smatz@8596: } truelight@201: } tron@3066: break; tron@3066: tron@3066: default: break; truelight@0: } frosch@8616: return CombineTrackStatus(trackdirbits, red_signals); truelight@0: } truelight@0: truelight@0: static const StringID _road_tile_strings[] = { truelight@0: STR_1814_ROAD, truelight@0: STR_1814_ROAD, truelight@0: STR_1814_ROAD, truelight@0: STR_1815_ROAD_WITH_STREETLIGHTS, truelight@0: STR_1814_ROAD, truelight@0: STR_1816_TREE_LINED_ROAD, truelight@0: STR_1814_ROAD, truelight@0: STR_1814_ROAD, truelight@0: }; truelight@0: tron@1977: static void GetTileDesc_Road(TileIndex tile, TileDesc *td) truelight@0: { frosch@9322: Owner rail_owner = INVALID_OWNER; frosch@9322: Owner road_owner = INVALID_OWNER; frosch@9322: Owner tram_owner = INVALID_OWNER; frosch@9322: rubidium@3793: switch (GetRoadTileType(tile)) { frosch@9322: case ROAD_TILE_CROSSING: { frosch@9322: td->str = STR_1818_ROAD_RAIL_LEVEL_CROSSING; frosch@9322: RoadTypes rts = GetRoadTypes(tile); frosch@9322: rail_owner = GetTileOwner(tile); frosch@9322: if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD); frosch@9322: if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM); frosch@9322: break; frosch@9322: } frosch@9322: frosch@9322: case ROAD_TILE_DEPOT: frosch@9322: td->str = STR_1817_ROAD_VEHICLE_DEPOT; frosch@9322: road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter frosch@9322: break; frosch@9322: frosch@9322: default: { frosch@9322: RoadTypes rts = GetRoadTypes(tile); frosch@9322: td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_TRAMWAY); frosch@9322: if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD); frosch@9322: if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM); frosch@9322: break; frosch@9322: } frosch@9322: } frosch@9322: frosch@9322: /* Now we have to discover, if the tile has only one owner or many: frosch@9322: * - Find a first_owner of the tile. (Currently road or tram must be present, but this will break when the third type becomes available) frosch@9322: * - Compare the found owner with the other owners, and test if they differ. frosch@9322: * Note: If road exists it will be the first_owner. frosch@9322: */ frosch@9322: Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner); frosch@9322: bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner); frosch@9322: frosch@9322: if (mixed_owners) { frosch@9322: /* Multiple owners */ frosch@9322: td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_RAIL_OWNER); frosch@9322: td->owner[0] = rail_owner; frosch@9322: td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_ROAD_OWNER); frosch@9322: td->owner[1] = road_owner; frosch@9322: td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_TRAM_OWNER); frosch@9322: td->owner[2] = tram_owner; frosch@9322: } else { frosch@9322: /* One to rule them all */ frosch@9322: td->owner[0] = first_owner; tron@3069: } truelight@0: } truelight@0: maedhros@6723: /** maedhros@6723: * Given the direction the road depot is pointing, this is the direction the maedhros@6723: * vehicle should be travelling in in order to enter the depot. maedhros@6723: */ maedhros@6723: static const byte _roadveh_enter_depot_dir[4] = { maedhros@6723: TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE truelight@0: }; truelight@0: rubidium@8119: static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y) truelight@0: { rubidium@3793: switch (GetRoadTileType(tile)) { rubidium@3793: case ROAD_TILE_CROSSING: smatz@8342: if (v->type == VEH_TRAIN) { smatz@8342: /* it should be barred */ smatz@8342: assert(IsCrossingBarred(tile)); truelight@0: } tron@3060: break; tron@3060: rubidium@3793: case ROAD_TILE_DEPOT: rubidium@6259: if (v->type == VEH_ROAD && tron@4077: v->u.road.frame == 11 && maedhros@6723: _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == v->u.road.state) { maedhros@6857: v->u.road.state = RVSB_IN_DEPOT; maedhros@6857: v->vehstatus |= VS_HIDDEN; maedhros@6857: v->direction = ReverseDir(v->direction); rubidium@7492: if (v->Next() == NULL) VehicleEnterDepot(v); maedhros@6857: v->tile = tile; maedhros@6857: maedhros@6857: InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); rubidium@5991: return VETSB_ENTERED_WORMHOLE; tron@3060: } tron@3060: break; tron@3060: tron@3060: default: break; truelight@0: } rubidium@5991: return VETSB_CONTINUE; truelight@0: } truelight@0: truelight@0: Darkvater@2436: static void ChangeTileOwner_Road(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { frosch@8563: if (IsRoadDepot(tile)) { rubidium@6717: if (GetTileOwner(tile) == old_player) { rubidium@6717: if (new_player == PLAYER_SPECTATOR) { smatz@8519: DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR); rubidium@6717: } else { rubidium@6717: SetTileOwner(tile, new_player); rubidium@6717: } rubidium@6689: } rubidium@6661: return; truelight@0: } truelight@0: rubidium@6661: for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) { frosch@9341: /* Update all roadtypes, no matter if they are present */ rubidium@6661: if (GetRoadOwner(tile, rt) == old_player) { rubidium@6661: SetRoadOwner(tile, rt, new_player == PLAYER_SPECTATOR ? OWNER_NONE : new_player); rubidium@6661: } rubidium@6661: } tron@3060: rubidium@6661: if (IsLevelCrossing(tile)) { rubidium@6689: if (GetTileOwner(tile) == old_player) { rubidium@6689: if (new_player == PLAYER_SPECTATOR) { smatz@8598: DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL); rubidium@6689: } else { rubidium@6689: SetTileOwner(tile, new_player); rubidium@6689: } rubidium@6689: } truelight@0: } truelight@0: } truelight@0: rubidium@7494: static CommandCost TerraformTile_Road(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new) rubidium@7494: { rubidium@9413: if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) { rubidium@7582: switch (GetRoadTileType(tile)) { rubidium@7582: case ROAD_TILE_CROSSING: rubidium@8230: if (!IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new)) && HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@7582: break; rubidium@7582: rubidium@7582: case ROAD_TILE_DEPOT: rubidium@8230: if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@7582: break; rubidium@7582: rubidium@7582: case ROAD_TILE_NORMAL: { rubidium@7582: RoadBits bits = GetAllRoadBits(tile); rubidium@7582: RoadBits bits_copy = bits; rubidium@7582: /* Check if the slope-road_bits combination is valid at all, i.e. it is save to call GetRoadFoundation(). */ skidd13@8744: if (!CmdFailed(CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE))) { rubidium@7582: /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */ rubidium@7582: if (bits == bits_copy) { rubidium@7582: uint z_old; rubidium@7582: Slope tileh_old = GetTileSlope(tile, &z_old); rubidium@7582: rubidium@7582: /* Get the slope on top of the foundation */ rubidium@7582: z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old); rubidium@7582: z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new); rubidium@7582: rubidium@7582: /* The surface slope must not be changed */ rubidium@8230: if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@7582: } rubidium@7582: } rubidium@7582: break; rubidium@7582: } rubidium@7582: rubidium@7582: default: NOT_REACHED(); rubidium@7582: } rubidium@7582: } rubidium@7582: rubidium@7494: return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@7494: } rubidium@7494: truelight@0: rubidium@5587: extern const TileTypeProcs _tile_type_road_procs = { rubidium@4344: DrawTile_Road, /* draw_tile_proc */ rubidium@4344: GetSlopeZ_Road, /* get_slope_z_proc */ rubidium@4344: ClearTile_Road, /* clear_tile_proc */ rubidium@4344: GetAcceptedCargo_Road, /* get_accepted_cargo_proc */ rubidium@4344: GetTileDesc_Road, /* get_tile_desc_proc */ rubidium@4344: GetTileTrackStatus_Road, /* get_tile_track_status_proc */ rubidium@4344: ClickTile_Road, /* click_tile_proc */ rubidium@4344: AnimateTile_Road, /* animate_tile_proc */ rubidium@4344: TileLoop_Road, /* tile_loop_clear */ rubidium@4344: ChangeTileOwner_Road, /* change_tile_owner_clear */ rubidium@4344: NULL, /* get_produced_cargo_proc */ rubidium@4344: VehicleEnter_Road, /* vehicle_enter_tile_proc */ rubidium@7335: GetFoundation_Road, /* get_foundation_proc */ rubidium@7494: TerraformTile_Road, /* terraform_tile_proc */ truelight@0: };