tron@2186: /* $Id$ */ tron@2186: rubidium@10429: /** @file road_cmd.cpp Commands related to road tiles. */ belugas@6889: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@3189: #include "bridge_map.h" rubidium@6486: #include "bridge.h" tron@6460: #include "cmd_helper.h" tron@3101: #include "rail_map.h" tron@3144: #include "road_map.h" rubidium@8598: #include "road_internal.h" tron@4232: #include "sprite.h" rubidium@8615: #include "tile_cmd.h" maedhros@6669: #include "landscape.h" tron@3319: #include "town_map.h" rubidium@8720: #include "viewport_func.h" rubidium@8612: #include "command_func.h" truelight@0: #include "town.h" KUDr@3900: #include "yapf/yapf.h" rubidium@10222: #include "depot_base.h" rubidium@10222: #include "depot_func.h" maedhros@7037: #include "newgrf.h" rubidium@7175: #include "station_map.h" rubidium@7175: #include "tunnel_map.h" rubidium@8707: #include "variables.h" rubidium@8078: #include "autoslope.h" belugas@8345: #include "transparency.h" smatz@8579: #include "tunnelbridge_map.h" rubidium@8602: #include "window_func.h" rubidium@8610: #include "strings_func.h" rubidium@8640: #include "vehicle_func.h" rubidium@8640: #include "vehicle_base.h" rubidium@8653: #include "sound_func.h" smatz@8838: #include "road_func.h" smatz@8894: #include "tunnelbridge.h" rubidium@10225: #include "cheat_func.h" rubidium@10269: #include "functions.h" rubidium@10272: #include "effectvehicle_func.h" smatz@10473: #include "elrail_func.h" rubidium@10316: #include "oldpool_func.h" smatz@8579: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" rubidium@8078: rubidium@10622: rubidium@10622: bool RoadVehiclesAreBuilt() rubidium@10622: { rubidium@10622: const Vehicle* v; rubidium@10622: rubidium@10622: FOR_ALL_VEHICLES(v) { rubidium@10622: if (v->type == VEH_ROAD) return true; rubidium@10622: } rubidium@10622: return false; rubidium@10622: } rubidium@10622: rubidium@10622: /** rubidium@10622: * Change the side of the road vehicles drive on (server only). rubidium@10622: * @param tile unused rubidium@10622: * @param flags operation to perform rubidium@10622: * @param p1 the side of the road; 0 = left side and 1 = right side rubidium@10622: * @param p2 unused rubidium@10622: */ rubidium@10622: CommandCost CmdSetRoadDriveSide(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) rubidium@10622: { rubidium@10622: /* Check boundaries and you can only change this if NO vehicles have been built yet, rubidium@10622: * except in the intro-menu where of course it's always possible to do so. */ rubidium@10622: if (p1 > 1 || (_game_mode != GM_MENU && RoadVehiclesAreBuilt())) return CMD_ERROR; rubidium@10622: rubidium@10622: if (flags & DC_EXEC) { rubidium@10695: if (_game_mode == GM_MENU) { glx@10828: _settings_newgame.vehicle.road_side = p1; rubidium@10695: } else { rubidium@10775: _settings_game.vehicle.road_side = p1; rubidium@10695: } rubidium@10622: InvalidateWindow(WC_GAME_OPTIONS, 0); rubidium@10622: } rubidium@10622: return CommandCost(); rubidium@10622: } rubidium@10622: rubidium@8078: #define M(x) (1 << (x)) rubidium@8078: /* Level crossings may only be built on these slopes */ rubidium@8078: 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@8078: #undef M truelight@0: skidd13@9240: /* Invalid RoadBits on slopes */ skidd13@9240: static const RoadBits _invalid_tileh_slopes_road[2][15] = { skidd13@9240: /* The inverse of the mixable RoadBits on a leveled slope */ skidd13@9240: { skidd13@9240: ROAD_NONE, // SLOPE_FLAT skidd13@9240: ROAD_NE | ROAD_SE, // SLOPE_W skidd13@9240: ROAD_NE | ROAD_NW, // SLOPE_S skidd13@9240: skidd13@9240: ROAD_NE, // SLOPE_SW skidd13@9240: ROAD_NW | ROAD_SW, // SLOPE_E skidd13@9240: ROAD_NONE, // SLOPE_EW skidd13@9240: skidd13@9240: ROAD_NW, // SLOPE_SE skidd13@9240: ROAD_NONE, // SLOPE_WSE skidd13@9240: ROAD_SE | ROAD_SW, // SLOPE_N skidd13@9240: skidd13@9240: ROAD_SE, // SLOPE_NW skidd13@9240: ROAD_NONE, // SLOPE_NS skidd13@9240: ROAD_NONE, // SLOPE_ENW skidd13@9240: skidd13@9240: ROAD_SW, // SLOPE_NE skidd13@9240: ROAD_NONE, // SLOPE_SEN skidd13@9240: ROAD_NONE // SLOPE_NWS skidd13@9240: }, skidd13@9240: /* The inverse of the allowed straight roads on a slope skidd13@9240: * (with and without a foundation). */ skidd13@9240: { skidd13@9240: ROAD_NONE, // SLOPE_FLAT skidd13@9240: ROAD_NONE, // SLOPE_W Foundation skidd13@9240: ROAD_NONE, // SLOPE_S Foundation skidd13@9240: skidd13@9240: ROAD_Y, // SLOPE_SW skidd13@9240: ROAD_NONE, // SLOPE_E Foundation skidd13@9240: ROAD_ALL, // SLOPE_EW skidd13@9240: skidd13@9240: ROAD_X, // SLOPE_SE skidd13@9240: ROAD_ALL, // SLOPE_WSE skidd13@9240: ROAD_NONE, // SLOPE_N Foundation skidd13@9240: skidd13@9240: ROAD_X, // SLOPE_NW skidd13@9240: ROAD_ALL, // SLOPE_NS skidd13@9240: ROAD_ALL, // SLOPE_ENW skidd13@9240: skidd13@9240: ROAD_Y, // SLOPE_NE skidd13@9240: ROAD_ALL, // SLOPE_SEN skidd13@9240: ROAD_ALL // SLOPE_NW skidd13@9240: } skidd13@9240: }; skidd13@9240: smatz@8880: Foundation GetRoadFoundation(Slope tileh, RoadBits bits); smatz@8880: frosch@10690: /** frosch@10690: * Is it allowed to remove the given road bits from the given tile? frosch@10690: * @param tile the tile to remove the road from frosch@10690: * @param remove the roadbits that are going to be removed frosch@10690: * @param owner the actual owner of the roadbits of the tile frosch@10690: * @param rt the road type to remove the bits from frosch@10690: * @param flags command flags frosch@10690: * @param town_check Shall the town rating checked/affected frosch@10690: * @return true when it is allowed to remove the road bits frosch@10690: */ frosch@10690: bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, RoadType rt, uint32 flags, bool town_check) truelight@0: { rubidium@7157: if (_game_mode == GM_EDITOR || remove == ROAD_NONE) return true; truelight@0: rubidium@7247: /* Water can always flood and towns can always remove "normal" road pieces. rubidium@7247: * Towns are not be allowed to remove non "normal" road pieces, like tram rubidium@7247: * tracks as that would result in trams that cannot turn. */ rubidium@7247: if (_current_player == OWNER_WATER || rubidium@11161: (rt == ROADTYPE_ROAD && !IsValidPlayerID(_current_player))) return true; truelight@0: belugas@6889: /* Only do the special processing if the road is owned belugas@6889: * by a town */ Darkvater@4849: if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner); truelight@0: frosch@10690: if (!town_check) return true; frosch@10690: tron@3017: if (_cheats.magic_bulldozer.value) return true; truelight@0: frosch@10690: Town *t = ClosestTownFromTile(tile, UINT_MAX); frosch@10690: if (t == NULL) return true; frosch@10690: frosch@10690: /* check if you're allowed to remove the street owned by a town frosch@10690: * removal allowance depends on difficulty setting */ frosch@10690: if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return false; frosch@10690: belugas@6889: /* Get a bitmask of which neighbouring roads has a tile */ skidd13@9230: RoadBits n = ROAD_NONE; skidd13@9230: RoadBits present = GetAnyRoadBits(tile, rt); rubidium@7157: if (present & ROAD_NE && GetAnyRoadBits(TILE_ADDXY(tile, -1, 0), rt) & ROAD_SW) n |= ROAD_NE; rubidium@7157: if (present & ROAD_SE && GetAnyRoadBits(TILE_ADDXY(tile, 0, 1), rt) & ROAD_NW) n |= ROAD_SE; rubidium@7157: if (present & ROAD_SW && GetAnyRoadBits(TILE_ADDXY(tile, 1, 0), rt) & ROAD_NE) n |= ROAD_SW; rubidium@7157: if (present & ROAD_NW && GetAnyRoadBits(TILE_ADDXY(tile, 0, -1), rt) & ROAD_SE) n |= ROAD_NW; truelight@201: frosch@10690: int rating_decrease = RATING_ROAD_DOWN_STEP_EDGE; belugas@6889: /* If 0 or 1 bits are set in n, or if no bits that match the bits to remove, belugas@6889: * then allow it */ skidd13@9230: if (KillFirstBit(n) != ROAD_NONE && (n & remove) != ROAD_NONE) { belugas@6889: /* you can remove all kind of roads with extra dynamite */ rubidium@10775: if (!_settings_game.construction.extra_dynamite) { frosch@10690: SetDParam(0, t->index); frosch@10690: _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; frosch@10690: return false; frosch@10690: } frosch@10690: rating_decrease = RATING_ROAD_DOWN_STEP_INNER; truelight@0: } frosch@10690: ChangeTownRating(t, rating_decrease, RATING_ROAD_MINIMUM); truelight@0: truelight@0: return true; truelight@0: } truelight@0: smatz@8924: Darkvater@1784: /** Delete a piece of road. tron@3491: * @param tile tile where to remove road from belugas@6979: * @param flags operation to perform smatz@8924: * @param pieces roadbits to remove smatz@8924: * @param rt roadtype to remove smatz@8924: * @param crossing_check should we check if there is a tram track when we are removing road from crossing? truelight@0: */ rubidium@10151: static CommandCost RemoveRoad(TileIndex tile, uint32 flags, RoadBits pieces, RoadType rt, bool crossing_check, bool town_check = true) truelight@0: { frosch@10689: RoadTypes rts = GetRoadTypes(tile); frosch@10689: /* The tile doesn't have the given road type */ frosch@10689: if (!HasBit(rts, rt)) return CMD_ERROR; frosch@10689: frosch@10690: bool town_road_under_stop = false; frosch@10690: rubidium@7175: switch (GetTileType(tile)) { rubidium@7866: case MP_ROAD: smatz@8571: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; rubidium@7175: break; rubidium@7175: rubidium@7175: case MP_STATION: rubidium@7175: if (!IsDriveThroughStopTile(tile)) return CMD_ERROR; frosch@10690: if (rt == ROADTYPE_ROAD) town_road_under_stop = GetStopBuiltOnTownRoad(tile); smatz@8571: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; rubidium@7175: break; rubidium@7175: rubidium@7175: case MP_TUNNELBRIDGE: smatz@8886: if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; smatz@8886: if (GetVehicleTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile)) != NULL) return CMD_ERROR; smatz@8886: break; rubidium@7175: rubidium@7175: default: rubidium@7175: return CMD_ERROR; rubidium@7175: } Darkvater@1638: frosch@10690: if (!CheckAllowRemoveRoad(tile, pieces, town_road_under_stop ? OWNER_TOWN : GetRoadOwner(tile, rt), rt, flags, town_check)) return CMD_ERROR; celestar@5573: rubidium@7866: if (!IsTileType(tile, MP_ROAD)) { rubidium@7175: /* If it's the last roadtype, just clear the whole tile */ rubidium@7175: if (rts == RoadTypeToRoadTypes(rt)) return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@7175: rubidium@8726: CommandCost cost(EXPENSES_CONSTRUCTION); rubidium@7175: if (IsTileType(tile, MP_TUNNELBRIDGE)) { smatz@8693: TileIndex other_end = GetOtherTunnelBridgeEnd(tile); rubidium@7175: /* Pay for *every* tile of the bridge or tunnel */ smatz@8894: cost.AddCost((GetTunnelBridgeLength(other_end, tile) + 2) * _price.remove_road); rubidium@7175: if (flags & DC_EXEC) { rubidium@7175: SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt)); rubidium@7175: SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt)); rubidium@7175: rubidium@7175: /* Mark tiles diry that have been repaved */ rubidium@7210: MarkTileDirtyByTile(tile); rubidium@7175: MarkTileDirtyByTile(other_end); rubidium@7175: if (IsBridge(tile)) { smatz@8579: TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile)); rubidium@7175: rubidium@7209: for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t); rubidium@7175: } rubidium@7175: } rubidium@7175: } else { rubidium@7446: cost.AddCost(_price.remove_road); rubidium@7175: if (flags & DC_EXEC) { rubidium@7175: SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt)); rubidium@7175: MarkTileDirtyByTile(tile); rubidium@7175: } rubidium@7175: } rubidium@8726: return cost; rubidium@7175: } rubidium@7175: celestar@5573: switch (GetRoadTileType(tile)) { celestar@5573: case ROAD_TILE_NORMAL: { skidd13@9240: const Slope tileh = GetTileSlope(tile, NULL); rubidium@7157: RoadBits present = GetRoadBits(tile, rt); skidd13@9240: const RoadBits other = GetOtherRoadBits(tile, rt); skidd13@9240: const Foundation f = GetRoadFoundation(tileh, present); celestar@5573: smatz@10378: if (HasRoadWorks(tile) && _current_player != OWNER_WATER) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS); celestar@5573: skidd13@9240: /* Autocomplete to a straight road skidd13@9240: * @li on steep slopes skidd13@9240: * @li if the bits of the other roadtypes result in another foundation skidd13@9240: * @li if build on slopes is disabled */ rubidium@9243: if (IsSteepSlope(tileh) || (IsStraightRoad(other) && rubidium@9243: (other & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) != ROAD_NONE) || rubidium@10775: (tileh != SLOPE_FLAT && !_settings_game.construction.build_on_slopes)) { skidd13@9230: pieces |= MirrorRoadBits(pieces); celestar@5573: } celestar@5573: belugas@6889: /* limit the bits to delete to the existing bits. */ skidd13@9230: pieces &= present; skidd13@9230: if (pieces == ROAD_NONE) return CMD_ERROR; celestar@5573: skidd13@9240: /* Now set present what it will be after the remove */ skidd13@9240: present ^= pieces; skidd13@9240: skidd13@9240: /* Check for invalid RoadBit combinations on slopes */ skidd13@9240: if (tileh != SLOPE_FLAT && present != ROAD_NONE && skidd13@9240: (present & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) == present) { skidd13@9240: return CMD_ERROR; skidd13@9240: } skidd13@9240: celestar@5573: if (flags & DC_EXEC) { smatz@10378: if (HasRoadWorks(tile)) { smatz@10378: /* flooding tile with road works, don't forget to remove the effect vehicle too */ smatz@10378: assert(_current_player == OWNER_WATER); smatz@10378: Vehicle *v; smatz@10378: FOR_ALL_VEHICLES(v) { smatz@10378: if (v->type == VEH_EFFECT && TileVirtXY(v->x_pos, v->y_pos) == tile) { smatz@10378: delete v; smatz@10378: } smatz@10378: } smatz@10378: } rubidium@7158: if (present == ROAD_NONE) { rubidium@7157: RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt)); rubidium@7157: if (rts == ROADTYPES_NONE) { rubidium@8032: /* Includes MarkTileDirtyByTile() */ rubidium@7157: DoClearSquare(tile); rubidium@7157: } else { rubidium@7158: SetRoadBits(tile, ROAD_NONE, rt); rubidium@7157: SetRoadTypes(tile, rts); rubidium@8032: MarkTileDirtyByTile(tile); rubidium@7157: } celestar@5573: } else { rubidium@7260: /* When bits are removed, you *always* end up with something that rubidium@7260: * is not a complete straight road tile. However, trams do not have rubidium@7260: * onewayness, so they cannot remove it either. */ rubidium@7260: if (rt != ROADTYPE_TRAM) SetDisallowedRoadDirections(tile, DRD_NONE); rubidium@7157: SetRoadBits(tile, present, rt); celestar@5573: MarkTileDirtyByTile(tile); celestar@5573: } celestar@5573: } skidd13@9240: skidd13@9240: /* If we change the foundation we have to pay for it. */ skidd13@9240: return CommandCost(EXPENSES_CONSTRUCTION, CountBits(pieces) * _price.remove_road + skidd13@9240: ((GetRoadFoundation(tileh, present) != f) ? _price.terraform : (Money)0)); celestar@5573: } celestar@5573: celestar@5573: case ROAD_TILE_CROSSING: { celestar@5573: if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) { tron@3176: return CMD_ERROR; truelight@0: } truelight@0: rubidium@7193: /* Don't allow road to be removed from the crossing when there is tram; rubidium@7193: * we can't draw the crossing without trambits ;) */ frosch@9059: if (rt == ROADTYPE_ROAD && HasTileRoadType(tile, ROADTYPE_TRAM) && (flags & DC_EXEC || crossing_check)) return CMD_ERROR; rubidium@7193: glx@8683: if (flags & DC_EXEC) { rubidium@7157: RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt)); rubidium@7157: if (rts == ROADTYPES_NONE) { rubidium@7157: MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GetRailType(tile)); rubidium@7157: } else { rubidium@7157: SetRoadTypes(tile, rts); rubidium@7157: } tron@3060: MarkTileDirtyByTile(tile); rubidium@5838: YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile))); truelight@0: } rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_road * 2); celestar@5573: } tron@3977: celestar@5573: default: celestar@5573: case ROAD_TILE_DEPOT: celestar@5573: return CMD_ERROR; truelight@0: } truelight@0: } truelight@0: truelight@0: smatz@8924: /** Delete a piece of road. smatz@8924: * @param tile tile where to remove road from smatz@8924: * @param flags operation to perform smatz@8924: * @param p1 bit 0..3 road pieces to remove (RoadBits) smatz@8924: * bit 4..5 road type smatz@8924: * @param p2 unused smatz@8924: */ smatz@8924: CommandCost CmdRemoveRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) smatz@8924: { smatz@8924: RoadType rt = (RoadType)GB(p1, 4, 2); smatz@8924: if (!IsValidRoadType(rt)) return CMD_ERROR; smatz@8924: smatz@8924: RoadBits pieces = Extract(p1); smatz@8924: smatz@8924: return RemoveRoad(tile, flags, pieces, rt, true); smatz@8924: } smatz@8924: rubidium@8137: /** rubidium@8137: * Calculate the costs for roads on slopes rubidium@8137: * Aside modify the RoadBits to fit on the slopes rubidium@8137: * rubidium@8137: * @note The RoadBits are modified too! rubidium@8137: * @param tileh The current slope rubidium@8137: * @param pieces The RoadBits we want to add skidd13@9240: * @param existing The existent RoadBits of the current type skidd13@9240: * @param other The other existent RoadBits rubidium@8137: * @return The costs for these RoadBits on this slope rubidium@8137: */ smatz@9317: static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other) truelight@0: { skidd13@9240: /* Remove already build pieces */ skidd13@9240: CLRBITS(*pieces, existing); skidd13@9240: skidd13@9240: /* If we can't build anything stop here */ skidd13@9240: if (*pieces == ROAD_NONE) return CMD_ERROR; skidd13@9240: skidd13@9240: /* All RoadBit combos are valid on flat land */ skidd13@9240: if (tileh == SLOPE_FLAT) return CommandCost(); skidd13@9240: skidd13@9230: /* Proceed steep Slopes first to reduce lookup table size */ tron@4246: if (IsSteepSlope(tileh)) { rubidium@8137: /* Force straight roads. */ rubidium@8137: *pieces |= MirrorRoadBits(*pieces); rubidium@7828: skidd13@9240: /* Use existing as all existing since only straight skidd13@9240: * roads are allowed here. */ skidd13@9240: existing |= other; skidd13@9240: skidd13@9240: if ((existing == ROAD_NONE || existing == *pieces) && IsStraightRoad(*pieces)) { skidd13@9240: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); tron@4246: } tron@4246: return CMD_ERROR; tron@4246: } rubidium@8137: skidd13@9240: /* Save the merge of all bits of the current type */ skidd13@9240: RoadBits type_bits = existing | *pieces; rubidium@8137: skidd13@9240: /* Roads on slopes */ rubidium@10775: if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) { skidd13@9240: skidd13@9240: /* If we add leveling we've got to pay for it */ skidd13@9240: if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); skidd13@9240: skidd13@9240: return CommandCost(); rubidium@8137: } truelight@0: skidd13@9240: /* Autocomplete uphill roads */ skidd13@9240: *pieces |= MirrorRoadBits(*pieces); skidd13@9240: type_bits = existing | *pieces; tron@3381: skidd13@9240: /* Uphill roads */ skidd13@9240: if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) && skidd13@9240: (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) { tron@3381: skidd13@9240: /* Slopes with foundation ? */ skidd13@9240: if (IsSlopeWithOneCornerRaised(tileh)) { rubidium@7828: skidd13@9240: /* Prevent build on slopes if it isn't allowed */ rubidium@10775: if (_settings_game.construction.build_on_slopes) { skidd13@9240: skidd13@9240: /* If we add foundation we've got to pay for it */ skidd13@9240: if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); skidd13@9240: skidd13@9240: return CommandCost(); skidd13@9240: } skidd13@9240: } else { skidd13@9240: if (CountBits(existing) == 1) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); skidd13@9240: return CommandCost(); skidd13@9240: } 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@6979: * @param flags operation to perform tron@6460: * @param p1 bit 0..3 road pieces to build (RoadBits) rubidium@7157: * bit 4..5 road type rubidium@7260: * 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@7439: CommandCost CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@8726: CommandCost cost(EXPENSES_CONSTRUCTION); smatz@9317: rubidium@5838: RoadBits existing = ROAD_NONE; skidd13@9240: 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@11161: if ((IsValidPlayerID(_current_player) && p2 != 0) || (_current_player == OWNER_TOWN && !IsValidTownID(p2))) return CMD_ERROR; tron@6460: tron@6460: RoadBits pieces = Extract(p1); Darkvater@1784: smatz@9186: /* do not allow building 'zero' road bits, code wouldn't handle it */ smatz@9186: if (pieces == ROAD_NONE) return CMD_ERROR; smatz@9186: rubidium@7157: RoadType rt = (RoadType)GB(p1, 4, 2); truelight@8353: if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR; rubidium@7157: rubidium@7260: DisallowedRoadDirections toggle_drd = (DisallowedRoadDirections)GB(p1, 6, 2); rubidium@7260: skidd13@9230: Slope tileh = GetTileSlope(tile, NULL); truelight@0: celestar@3434: switch (GetTileType(tile)) { rubidium@7866: case MP_ROAD: rubidium@3793: switch (GetRoadTileType(tile)) { rubidium@7260: case ROAD_TILE_NORMAL: { tron@4046: if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS); rubidium@7252: skidd13@9240: other_bits = GetOtherRoadBits(tile, rt); frosch@9059: if (!HasTileRoadType(tile, rt)) break; tron@4046: rubidium@7157: existing = GetRoadBits(tile, rt); skidd13@9230: bool crossing = !IsStraightRoad(existing | pieces); rubidium@7264: if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) { rubidium@7260: /* Junctions cannot be one-way */ rubidium@7260: return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION); rubidium@7260: } tron@3150: if ((existing & pieces) == pieces) { rubidium@7260: /* We only want to set the (dis)allowed road directions */ frosch@10689: if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM && IsRoadOwner(tile, ROADTYPE_ROAD, _current_player)) { rubidium@7260: if (crossing) return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION); rubidium@7260: smatz@9097: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; smatz@9097: rubidium@7260: /* Ignore half built tiles */ skidd13@9230: if (flags & DC_EXEC && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) { rubidium@7260: SetDisallowedRoadDirections(tile, GetDisallowedRoadDirections(tile) ^ toggle_drd); rubidium@7260: MarkTileDirtyByTile(tile); rubidium@7260: } rubidium@7446: return CommandCost(); rubidium@7260: } tron@3066: return_cmd_error(STR_1007_ALREADY_BUILT); tron@3066: } rubidium@7260: } break; tron@3066: rubidium@3793: case ROAD_TILE_CROSSING: frosch@9059: if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT); skidd13@9240: other_bits = GetCrossingRoadBits(tile); skidd13@9240: if (pieces & ComplementRoadBits(other_bits)) goto do_clear; rubidium@7157: 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@8424: 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@9317: smatz@9317: 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@6902: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; tron@3381: tron@3060: if (flags & DC_EXEC) { rubidium@5838: YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile))); rubidium@7193: /* Always add road to the roadtypes (can't draw without it) */ rubidium@7193: MakeRoadCrossing(tile, _current_player, _current_player, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2); smatz@8840: UpdateLevelCrossing(tile, false); tron@3100: MarkTileDirtyByTile(tile); tron@3060: } rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.build_road * (rt == ROADTYPE_ROAD ? 2 : 4)); tron@3017: } truelight@0: rubidium@7175: case MP_STATION: rubidium@7175: if (!IsDriveThroughStopTile(tile)) return CMD_ERROR; frosch@9059: if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT); rubidium@7175: break; rubidium@7175: rubidium@7175: case MP_TUNNELBRIDGE: smatz@8886: if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR; rubidium@11064: if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) return CMD_ERROR; frosch@9059: if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT); smatz@8886: /* Don't allow adding roadtype to the bridge/tunnel when vehicles are already driving on it */ smatz@8886: if (GetVehicleTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile)) != NULL) return CMD_ERROR; smatz@8886: break; rubidium@7175: smatz@9317: default: { truelight@0: do_clear:; smatz@9317: CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3381: if (CmdFailed(ret)) return ret; rubidium@7446: cost.AddCost(ret); smatz@9317: } break; truelight@0: } truelight@0: skidd13@9240: if (other_bits != pieces) { rubidium@7211: /* Check the foundation/slopes when adding road/tram bits */ smatz@9317: CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits); rubidium@7211: /* Return an error if we need to build a foundation (ret != 0) but the rubidium@7211: * current patch-setting is turned off (or stupid AI@work) */ rubidium@10775: if (CmdFailed(ret) || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) { rubidium@7211: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); rubidium@7211: } rubidium@7446: cost.AddCost(ret); rubidium@7211: } truelight@0: rubidium@7866: if (IsTileType(tile, MP_ROAD)) { belugas@6889: /* Don't put the pieces that already exist */ tron@3150: pieces &= ComplementRoadBits(existing); smatz@8880: smatz@8880: /* Check if new road bits will have the same foundation as other existing road types */ frosch@9059: if (IsNormalRoad(tile)) { smatz@8880: Slope slope = GetTileSlope(tile, NULL); smatz@8880: Foundation found_new = GetRoadFoundation(slope, pieces | existing); smatz@8880: smatz@8880: /* Test if all other roadtypes can be built at that foundation */ smatz@8880: for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) { smatz@8880: if (rtest != rt) { // check only other road types smatz@8880: RoadBits bits = GetRoadBits(tile, rtest); smatz@8880: /* do not check if there are not road bits of given type */ smatz@8880: if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) { smatz@8880: return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); smatz@8880: } smatz@8880: } smatz@8880: } smatz@8880: } truelight@0: } truelight@0: smatz@9097: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; smatz@9097: truelight@8328: cost.AddCost(CountBits(pieces) * _price.build_road); rubidium@7175: if (IsTileType(tile, MP_TUNNELBRIDGE)) { rubidium@7175: /* Pay for *every* tile of the bridge or tunnel */ smatz@8894: cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2); rubidium@7175: } truelight@0: truelight@0: if (flags & DC_EXEC) { rubidium@7175: switch (GetTileType(tile)) { rubidium@7866: case MP_ROAD: { rubidium@7175: RoadTileType rtt = GetRoadTileType(tile); rubidium@7175: if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) { rubidium@7175: SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt)); rubidium@7175: SetRoadOwner(tile, rt, _current_player); rubidium@7464: if (_current_player == OWNER_TOWN && rt == ROADTYPE_ROAD) SetTownIndex(tile, p2); rubidium@7175: } rubidium@7175: if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt); rubidium@7175: } break; rubidium@7175: rubidium@7175: case MP_TUNNELBRIDGE: { smatz@8693: TileIndex other_end = GetOtherTunnelBridgeEnd(tile); rubidium@7175: rubidium@7175: SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt)); rubidium@7157: SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt)); rubidium@7175: rubidium@7175: /* Mark tiles diry that have been repaved */ rubidium@7175: MarkTileDirtyByTile(other_end); rubidium@7210: MarkTileDirtyByTile(tile); rubidium@7175: if (IsBridge(tile)) { smatz@8579: TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile)); rubidium@7175: rubidium@7209: for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t); rubidium@7175: } rubidium@7175: } break; rubidium@7175: rubidium@7175: case MP_STATION: rubidium@7175: SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt)); frosch@10690: if (IsDriveThroughStopTile(tile) && rt == ROADTYPE_ROAD) SetStopBuiltOnTownRoad(tile, false); rubidium@7175: break; rubidium@7175: rubidium@7175: default: rubidium@7175: MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_player, _current_player, _current_player); rubidium@7175: break; truelight@0: } truelight@0: frosch@9059: if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) { rubidium@7260: existing |= pieces; skidd13@9230: SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ? rubidium@7260: GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE); rubidium@7260: } rubidium@7260: 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@6979: * @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@7158: * - p2 = (bit 3 + 4) - road type rubidium@7260: * - p2 = (bit 5) - set road direction Darkvater@1784: */ rubidium@7439: CommandCost CmdBuildLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { smatz@9317: CommandCost cost(EXPENSES_CONSTRUCTION); rubidium@7206: bool had_bridge = false; rubidium@7924: bool had_tunnel = false; rubidium@7260: bool had_success = false; rubidium@7260: DisallowedRoadDirections drd = DRD_NORTHBOUND; truelight@0: smatz@9097: _error_message = INVALID_STRING_ID; smatz@9097: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1784: smatz@9317: TileIndex start_tile = p1; rubidium@7158: RoadType rt = (RoadType)GB(p2, 3, 2); truelight@8353: 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@8424: if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis skidd13@8424: 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@8424: 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@8450: p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0; rubidium@7260: drd = DRD_SOUTHBOUND; truelight@0: } truelight@0: rubidium@7260: /* On the X-axis, we have to swap the initial bits, so they rubidium@7260: * will be interpreted correctly in the GTTS. Futhermore rubidium@7260: * when you just 'click' on one tile to build them. */ skidd13@8424: if (HasBit(p2, 2) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH; rubidium@7260: /* No disallowed direction bits have to be toggled */ skidd13@8424: if (!HasBit(p2, 5)) drd = DRD_NONE; rubidium@7260: smatz@9317: TileIndex tile = start_tile; belugas@6889: /* Start tile is the small number. */ Darkvater@1784: for (;;) { skidd13@8424: RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X; tron@3098: skidd13@8424: if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE; skidd13@8424: if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW; truelight@0: rubidium@11064: _error_message = INVALID_STRING_ID; smatz@9317: 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@7260: had_success = true; rubidium@7924: /* Only pay for the upgrade on one side of the bridges and tunnels */ rubidium@7924: if (IsTileType(tile, MP_TUNNELBRIDGE)) { rubidium@7924: if (IsBridge(tile)) { smatz@8579: if ((!had_bridge || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) { rubidium@7924: cost.AddCost(ret); rubidium@7924: } rubidium@7924: had_bridge = true; smatz@8886: } else { // IsTunnel(tile) smatz@8579: if ((!had_tunnel || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) { rubidium@7924: cost.AddCost(ret); rubidium@7924: } rubidium@7924: had_tunnel = true; rubidium@7206: } rubidium@7206: } else { rubidium@7446: cost.AddCost(ret); rubidium@7206: } tron@2549: } truelight@0: Darkvater@1784: if (tile == end_tile) break; truelight@0: skidd13@8424: tile += HasBit(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0); truelight@0: } truelight@0: rubidium@7260: 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@6979: * @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@7158: * - p2 = (bit 3 + 4) - road type Darkvater@1784: */ rubidium@7439: CommandCost CmdRemoveLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { smatz@9317: CommandCost cost(EXPENSES_CONSTRUCTION); darkvater@889: tron@2934: if (p1 >= MapSize()) return CMD_ERROR; Darkvater@1784: smatz@9317: TileIndex start_tile = p1; rubidium@7158: RoadType rt = (RoadType)GB(p2, 3, 2); rubidium@7158: if (!IsValidRoadType(rt)) return CMD_ERROR; truelight@0: Darkvater@1784: /* Only drag in X or Y direction dictated by the direction variable */ skidd13@8424: if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis skidd13@8424: 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@8424: 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@8450: p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0; truelight@0: } truelight@0: smatz@9317: Money money = GetAvailableMoneyForCommand(); smatz@9317: TileIndex tile = start_tile; belugas@6889: /* Start tile is the small number. */ Darkvater@1784: for (;;) { skidd13@8424: RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X; tron@3098: skidd13@8424: if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE; skidd13@8424: if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW; truelight@201: belugas@6889: /* try to remove the halves. */ tron@3098: if (bits != 0) { smatz@9317: CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true); rubidium@7938: if (CmdSucceeded(ret)) { rubidium@7938: if (flags & DC_EXEC) { rubidium@8726: money -= ret.GetCost(); rubidium@8726: if (money < 0) { rubidium@7938: _additional_cash_required = DoCommand(end_tile, start_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost(); rubidium@7938: return cost; rubidium@7938: } rubidium@10151: RemoveRoad(tile, flags, bits, rt, true, false); rubidium@7938: } rubidium@7938: cost.AddCost(ret); rubidium@7938: } truelight@0: } truelight@0: Darkvater@1784: if (tile == end_tile) break; truelight@0: skidd13@8424: tile += HasBit(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0); truelight@0: } truelight@0: rubidium@7446: 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@6979: * @param flags operation to perform tron@6460: * @param p1 bit 0..1 entrance direction (DiagDirection) rubidium@7158: * 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@7439: CommandCost CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@6460: DiagDirection dir = Extract(p1); rubidium@7158: RoadType rt = (RoadType)GB(p1, 2, 2); rubidium@7158: truelight@8353: if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR; Darkvater@1784: smatz@9317: Slope tileh = GetTileSlope(tile, NULL); tron@3636: if (tileh != SLOPE_FLAT && ( rubidium@10775: !_settings_game.construction.build_on_slopes || tron@3636: IsSteepSlope(tileh) || tron@6460: !CanBuildDepotByTileh(dir, tileh) tron@2549: )) { tron@2549: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: smatz@9317: CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); Darkvater@1784: if (CmdFailed(cost)) return CMD_ERROR; truelight@0: celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5573: rubidium@10314: if (!Depot::CanAllocateItem()) return CMD_ERROR; truelight@0: truelight@201: if (flags & DC_EXEC) { rubidium@10314: Depot *dep = new Depot(tile); truelight@0: dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index; truelight@0: rubidium@7157: MakeRoadDepot(tile, _current_player, dir, rt); tron@3100: MarkTileDirtyByTile(tile); truelight@0: } rubidium@7446: return cost.AddCost(_price.build_road_depot); truelight@0: } truelight@0: rubidium@7439: static CommandCost RemoveRoadDepot(TileIndex tile, uint32 flags) truelight@0: { smatz@9317: if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER) return CMD_ERROR; truelight@0: rubidium@8254: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; truelight@0: rubidium@7885: if (flags & DC_EXEC) { rubidium@7885: DoClearSquare(tile); rubidium@7885: delete GetDepotByTile(tile); rubidium@7885: } truelight@0: rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_road_depot); truelight@0: } truelight@0: rubidium@7439: static CommandCost ClearTile_Road(TileIndex tile, byte flags) tron@1977: { rubidium@3793: switch (GetRoadTileType(tile)) { rubidium@3793: case ROAD_TILE_NORMAL: { rubidium@7157: RoadBits b = GetAllRoadBits(tile); truelight@0: Darkvater@5888: /* Clear the road if only one piece is on the tile OR the AI tries Darkvater@5888: * to clear town road OR we are not using the DC_AUTO flag */ truelight@8328: if ((CountBits(b) == 1 && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || frosch@10689: ((flags & DC_AI_BUILDING) && GetOtherRoadBits(tile, ROADTYPE_ROAD) == ROAD_NONE && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) || Darkvater@5888: !(flags & DC_AUTO) Darkvater@5888: ) { rubidium@7175: RoadTypes rts = GetRoadTypes(tile); rubidium@8726: CommandCost ret(EXPENSES_CONSTRUCTION); rubidium@7175: for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) { skidd13@8424: if (HasBit(rts, rt)) { smatz@8924: CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true); rubidium@7175: if (CmdFailed(tmp_ret)) return tmp_ret; rubidium@7446: ret.AddCost(tmp_ret); rubidium@7175: } rubidium@7175: } rubidium@7175: return ret; tron@3060: } Darkvater@5888: return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST); Darkvater@5894: } truelight@0: rubidium@3793: case ROAD_TILE_CROSSING: { rubidium@7310: RoadTypes rts = GetRoadTypes(tile); rubidium@8726: CommandCost ret(EXPENSES_CONSTRUCTION); tron@3060: tron@3069: if (flags & DC_AUTO) return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST); tron@3060: rubidium@7310: /* Must iterate over the roadtypes in a reverse manner because rubidium@7310: * tram tracks must be removed before the road bits. */ rubidium@10085: RoadType rt = ROADTYPE_HWAY; rubidium@10085: do { skidd13@8424: if (HasBit(rts, rt)) { smatz@8924: CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false); rubidium@7310: if (CmdFailed(tmp_ret)) return tmp_ret; rubidium@7446: ret.AddCost(tmp_ret); rubidium@7310: } rubidium@10085: } 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@6574: struct DrawRoadTileStruct { truelight@0: uint16 image; truelight@0: byte subcoord_x; truelight@0: byte subcoord_y; rubidium@6574: }; truelight@0: truelight@0: #include "table/road_land.h" truelight@0: skidd13@9230: /** skidd13@9230: * Get the foundationtype of a RoadBits Slope combination skidd13@9230: * skidd13@9230: * @param tileh The Slope part skidd13@9230: * @param bits The RoadBits part skidd13@9230: * @return The resulting Foundation skidd13@9230: */ rubidium@7831: Foundation GetRoadFoundation(Slope tileh, RoadBits bits) tron@2548: { skidd13@9230: /* Flat land and land without a road doesn't require a foundation */ skidd13@9230: if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE; skidd13@9230: rubidium@7831: if (!IsSteepSlope(tileh)) { skidd13@9240: /* Leveled RoadBits on a slope */ skidd13@9240: if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED; skidd13@9240: skidd13@9240: /* Straight roads without foundation on a slope */ skidd13@9240: if (!IsSlopeWithOneCornerRaised(tileh) && skidd13@9240: (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE) skidd13@9240: return FOUNDATION_NONE; tron@4246: } truelight@0: skidd13@9240: /* Roads on steep Slopes or on Slopes with one corner raised */ rubidium@7831: 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@7037: * Whether to draw unpaved roads regardless of the town zone. maedhros@7037: * By default, OpenTTD always draws roads as unpaved if they are on a desert maedhros@7037: * tile or above the snowline. Newgrf files, however, can set a bit that allows maedhros@7037: * paved roads to be built on desert tiles as they would be on grassy tiles. maedhros@7037: * maedhros@7037: * @param tile The tile the road is on maedhros@7037: * @param roadside What sort of road this is maedhros@7037: * @return True if the road should be drawn unpaved regardless of the roadside. maedhros@7037: */ maedhros@7037: static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside) maedhros@7037: { maedhros@7037: return (IsOnSnow(tile) && rubidium@10775: !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) && maedhros@7037: roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS)); maedhros@7037: } maedhros@7037: maedhros@7037: /** rubidium@7187: * Draws the catenary for the given tile rubidium@7187: * @param ti information about the tile (slopes, height etc) rubidium@7187: * @param tram the roadbits for the tram rubidium@7187: */ smatz@9317: void DrawTramCatenary(const TileInfo *ti, RoadBits tram) rubidium@7187: { smatz@9302: /* Do not draw catenary if it is invisible */ smatz@9302: if (IsInvisibilitySet(TO_CATENARY)) return; smatz@9302: rubidium@7187: /* Don't draw the catenary under a low bridge */ smatz@9024: if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) { rubidium@7187: uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile)); rubidium@7187: rubidium@7195: if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return; rubidium@7187: } rubidium@7187: rubidium@7187: SpriteID front; rubidium@7187: SpriteID back; rubidium@7187: rubidium@7187: if (ti->tileh != SLOPE_FLAT) { rubidium@7187: back = SPR_TRAMWAY_BACK_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1]; rubidium@7187: front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1]; rubidium@7187: } else { rubidium@7187: back = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram]; rubidium@7187: front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram]; rubidium@7187: } rubidium@7187: smatz@9024: AddSortableSpriteToDraw(back, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY)); smatz@9024: AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY)); rubidium@7187: } rubidium@7187: rubidium@7187: /** rubidium@7260: * Draws details on/around the road rubidium@7260: * @param img the sprite to draw rubidium@7260: * @param ti the tile to draw on rubidium@7260: * @param dx the offset from the top of the BB of the tile rubidium@7260: * @param dy the offset from the top of the BB of the tile rubidium@7260: * @param h the height of the sprite to draw rubidium@7260: */ smatz@9317: static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h) rubidium@7260: { rubidium@7260: int x = ti->x | dx; rubidium@7260: int y = ti->y | dy; rubidium@7260: byte z = ti->z; rubidium@7260: if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y); rubidium@7260: AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z); rubidium@7260: } rubidium@7260: rubidium@7260: /** peter1138@2471: * Draw ground sprite and road pieces peter1138@2471: * @param ti TileInfo peter1138@2471: */ smatz@9317: static void DrawRoadBits(TileInfo *ti) peter1138@2471: { rubidium@7157: RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD); rubidium@7187: RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM); rubidium@7187: peter1138@5919: SpriteID image = 0; peter1138@5919: SpriteID pal = PAL_NONE; peter1138@2471: tron@3636: if (ti->tileh != SLOPE_FLAT) { rubidium@7831: DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram)); peter1138@2471: belugas@6889: /* DrawFoundation() modifies ti. belugas@6889: * Default sloped sprites.. */ tron@3636: if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F; peter1138@2471: } peter1138@2471: rubidium@7187: if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram]; peter1138@2471: smatz@9317: Roadside roadside = GetRoadside(ti->tile); peter1138@2471: maedhros@7037: if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) { peter1138@2471: image += 19; tron@4048: } else { tron@4048: switch (roadside) { peter1138@5919: 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@5919: DrawGroundSprite(image, pal); peter1138@2471: rubidium@7187: /* For tram we overlay the road graphics with either tram tracks only rubidium@7187: * (when there is actual road beneath the trams) or with tram tracks rubidium@7187: * and some dirts which hides the road graphics */ rubidium@7187: if (tram != ROAD_NONE) { rubidium@7187: if (ti->tileh != SLOPE_FLAT) { rubidium@7187: image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET; rubidium@7187: } else { rubidium@7187: image = _road_tile_sprites_1[tram] - SPR_ROAD_Y; rubidium@7187: } rubidium@7187: image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY; rubidium@7187: DrawGroundSprite(image, pal); rubidium@7187: } rubidium@7187: rubidium@7461: if (road != ROAD_NONE) { rubidium@7461: DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile); rubidium@7461: if (drd != DRD_NONE) { rubidium@7461: DrawRoadDetail(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), ti, 8, 8, 0); rubidium@7461: } rubidium@7461: } rubidium@7461: celestar@3430: if (HasRoadWorks(ti->tile)) { belugas@6889: /* Road works */ rubidium@7187: DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE); peter1138@2471: return; peter1138@2471: } peter1138@2471: rubidium@7187: if (tram != ROAD_NONE) DrawTramCatenary(ti, tram); rubidium@7187: belugas@6889: /* Return if full detail is disabled, or we are zoomed fully out. */ skidd13@8424: if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return; tron@5148: smatz@9085: /* Do not draw details (street lights, trees) under low bridge */ smatz@9085: if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) { smatz@9085: uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile)); smatz@9085: uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT; smatz@9085: smatz@9085: if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT; smatz@9085: smatz@9085: if (height < minz) return; smatz@9085: } smatz@9085: belugas@9143: /* If there are no road bits, return, as there is nothing left to do */ belugas@9143: if (CountBits(road) < 2) return; belugas@9143: belugas@6889: /* Draw extra details. */ smatz@9317: for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) { rubidium@7260: 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@7831: if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED); truelight@0: smatz@9317: SpriteID image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.crossing; smatz@9317: 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@9317: Roadside roadside = GetRoadside(ti->tile); smatz@9317: maedhros@7037: if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) { tron@3060: image += 8; tron@3060: } else { maedhros@7037: switch (roadside) { peter1138@5919: 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@5919: DrawGroundSprite(image, pal); frosch@9059: if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) { rubidium@7193: DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal); rubidium@7193: DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile)); rubidium@7193: } smatz@10473: if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti); tron@3060: break; truelight@0: } truelight@0: tron@3098: default: rubidium@3793: case ROAD_TILE_DEPOT: { rubidium@7831: if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED); tron@1398: smatz@9317: SpriteID palette = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile)); truelight@0: smatz@9317: const DrawTileSprites *dts; frosch@9059: if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) { rubidium@7187: dts = &_tram_depot[GetRoadDepotDirection(ti->tile)]; rubidium@7187: } else { rubidium@7187: dts = &_road_depot[GetRoadDepotDirection(ti->tile)]; rubidium@7187: } rubidium@7187: frosch@9067: DrawGroundSprite(dts->ground.sprite, PAL_NONE); tron@3060: smatz@9302: /* End now if buildings are invisible */ smatz@9302: if (IsInvisibilitySet(TO_BUILDINGS)) break; smatz@9302: smatz@9317: for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) { frosch@9066: SpriteID image = dtss->image.sprite; peter1138@5919: SpriteID pal; tron@3060: skidd13@8424: if (!IsTransparencySet(TO_BUILDINGS) && HasBit(image, PALETTE_MODIFIER_COLOR)) { peter1138@5919: pal = palette; peter1138@5919: } else { peter1138@5919: pal = PAL_NONE; tron@4226: } tron@3060: tron@4077: AddSortableSpriteToDraw( peter1138@5919: image, pal, tron@4232: ti->x + dtss->delta_x, ti->y + dtss->delta_y, tron@4232: dtss->size_x, dtss->size_y, rubidium@7829: dtss->size_z, ti->z, belugas@8345: IsTransparencySet(TO_BUILDINGS) tron@3060: ); tron@3060: } tron@3060: break; truelight@0: } truelight@0: } celestar@5573: DrawBridgeMiddle(ti); truelight@0: } truelight@0: rubidium@7162: void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt) truelight@0: { peter1138@5919: SpriteID palette = PLAYER_SPRITE_COLOR(_local_player); smatz@9317: 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@9067: DrawSprite(dts->ground.sprite, PAL_NONE, x, y); truelight@0: smatz@9317: 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@9066: SpriteID image = dtss->image.sprite; truelight@201: skidd13@8424: 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@9059: if (IsNormalRoad(tile)) { rubidium@7831: Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile)); rubidium@7831: 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@7831: static Foundation GetFoundation_Road(TileIndex tile, Slope tileh) dominik@39: { frosch@9059: if (IsNormalRoad(tile)) { rubidium@7831: return GetRoadFoundation(tileh, GetAllRoadBits(tile)); tron@3286: } else { rubidium@7831: 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@10775: switch (_settings_game.game_creation.landscape) { belugas@6683: case LT_ARCTIC: maedhros@6669: if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) { celestar@3430: ToggleSnow(tile); tron@3017: MarkTileDirtyByTile(tile); tron@3017: } tron@3017: break; tron@3017: belugas@6683: 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@9059: if (IsRoadDepot(tile)) return; truelight@0: smatz@9317: const Town *t = ClosestTownFromTile(tile, (uint)-1); celestar@3430: if (!HasRoadWorks(tile)) { belugas@8804: HouseZonesBits grp = HZB_TOWN_EDGE; truelight@1280: truelight@0: if (t != NULL) { truelight@0: grp = GetTownRadiusGroup(t, tile); truelight@201: belugas@6889: /* Show an animation to indicate road work */ truelight@201: if (t->road_build_months != 0 && belugas@8804: (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) && frosch@9059: IsNormalRoad(tile) && CountBits(GetAllRoadBits(tile)) > 1 ) { skidd13@8463: 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@10775: 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@8137: rubidium@10775: if (_settings_game.economy.mod_road_rebuild) { rubidium@8137: /* Generate a nicer town surface */ rubidium@8137: const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD); rubidium@8137: const RoadBits new_rb = CleanUpRoadBits(tile, old_rb); rubidium@8137: rubidium@8137: if (old_rb != new_rb) { smatz@8924: RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true); rubidium@8137: } rubidium@8137: } rubidium@8137: truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: tron@1977: static void ClickTile_Road(TileIndex tile) truelight@0: { frosch@9059: if (IsRoadDepot(tile)) ShowDepotWindow(tile, VEH_ROAD); truelight@0: } truelight@0: frosch@9112: /* 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@9112: static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side) tron@1977: { frosch@9112: TrackdirBits trackdirbits = TRACKDIR_BIT_NONE; frosch@9112: TrackdirBits red_signals = TRACKDIR_BIT_NONE; // crossing barred tron@3066: switch (mode) { tron@3066: case TRANSPORT_RAIL: frosch@9112: if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile)); frosch@9112: break; truelight@201: tron@3066: case TRANSPORT_ROAD: frosch@9112: if ((GetRoadTypes(tile) & sub_mode) == 0) break; rubidium@3793: switch (GetRoadTileType(tile)) { rubidium@7179: case ROAD_TILE_NORMAL: { rubidium@7260: const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 }; smatz@9092: RoadType rt = (RoadType)FindFirstBit(sub_mode); smatz@9092: RoadBits bits = GetRoadBits(tile, rt); smatz@9092: smatz@9092: /* no roadbit at this side of tile, return 0 */ frosch@9112: if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break; smatz@9092: rubidium@7260: uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)]; frosch@9112: if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier); frosch@9112: break; rubidium@7179: } tron@3066: rubidium@3793: case ROAD_TILE_CROSSING: { smatz@9092: Axis axis = GetCrossingRoadAxis(tile); tron@3272: frosch@9112: if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break; smatz@9092: frosch@9112: trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis)); frosch@9112: if (IsCrossingBarred(tile)) red_signals = trackdirbits; frosch@9112: break; tron@3066: } tron@3066: tron@3069: default: smatz@9092: case ROAD_TILE_DEPOT: { smatz@9092: DiagDirection dir = GetRoadDepotDirection(tile); smatz@9092: frosch@9112: if (side != INVALID_DIAGDIR && side != dir) break; smatz@9092: smatz@10546: trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir)); frosch@9112: break; smatz@9092: } truelight@201: } tron@3066: break; tron@3066: tron@3066: default: break; truelight@0: } frosch@9112: 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@10662: Owner rail_owner = INVALID_OWNER; frosch@10662: Owner road_owner = INVALID_OWNER; frosch@10662: Owner tram_owner = INVALID_OWNER; frosch@10662: rubidium@3793: switch (GetRoadTileType(tile)) { frosch@10662: case ROAD_TILE_CROSSING: { frosch@10662: td->str = STR_1818_ROAD_RAIL_LEVEL_CROSSING; frosch@10662: RoadTypes rts = GetRoadTypes(tile); frosch@10662: rail_owner = GetTileOwner(tile); frosch@10662: if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD); frosch@10662: if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM); frosch@10662: break; frosch@10662: } frosch@10662: frosch@10662: case ROAD_TILE_DEPOT: frosch@10662: td->str = STR_1817_ROAD_VEHICLE_DEPOT; frosch@10662: road_owner = GetTileOwner(tile); // Tile has only one owner, roadtype does not matter frosch@10662: break; frosch@10662: frosch@10662: default: { frosch@10662: RoadTypes rts = GetRoadTypes(tile); frosch@10662: td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_TRAMWAY); frosch@10662: if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD); frosch@10662: if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM); frosch@10662: break; frosch@10662: } frosch@10662: } frosch@10662: frosch@10662: /* Now we have to discover, if the tile has only one owner or many: frosch@10662: * - 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@10662: * - Compare the found owner with the other owners, and test if they differ. frosch@10662: * Note: If road exists it will be the first_owner. frosch@10662: */ frosch@10662: Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner); frosch@10662: bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner); frosch@10662: frosch@10662: if (mixed_owners) { frosch@10662: /* Multiple owners */ frosch@10662: td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_RAIL_OWNER); frosch@10662: td->owner[0] = rail_owner; frosch@10662: td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_ROAD_OWNER); frosch@10662: td->owner[1] = road_owner; frosch@10662: td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_TRAM_OWNER); frosch@10662: td->owner[2] = tram_owner; frosch@10662: } else { frosch@10662: /* One to rule them all */ frosch@10662: td->owner[0] = first_owner; tron@3069: } truelight@0: } truelight@0: maedhros@7219: /** maedhros@7219: * Given the direction the road depot is pointing, this is the direction the maedhros@7219: * vehicle should be travelling in in order to enter the depot. maedhros@7219: */ maedhros@7219: static const byte _roadveh_enter_depot_dir[4] = { maedhros@7219: TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE truelight@0: }; truelight@0: rubidium@8615: 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@8838: if (v->type == VEH_TRAIN) { smatz@8838: /* it should be barred */ smatz@8838: assert(IsCrossingBarred(tile)); truelight@0: } tron@3060: break; tron@3060: rubidium@3793: case ROAD_TILE_DEPOT: rubidium@6585: if (v->type == VEH_ROAD && tron@4077: v->u.road.frame == 11 && maedhros@7219: _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == v->u.road.state) { maedhros@7353: v->u.road.state = RVSB_IN_DEPOT; maedhros@7353: v->vehstatus |= VS_HIDDEN; maedhros@7353: v->direction = ReverseDir(v->direction); rubidium@7988: if (v->Next() == NULL) VehicleEnterDepot(v); maedhros@7353: v->tile = tile; maedhros@7353: maedhros@7353: InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); rubidium@6317: return VETSB_ENTERED_WORMHOLE; tron@3060: } tron@3060: break; tron@3060: tron@3060: default: break; truelight@0: } rubidium@6317: 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@9059: if (IsRoadDepot(tile)) { rubidium@7213: if (GetTileOwner(tile) == old_player) { rubidium@7213: if (new_player == PLAYER_SPECTATOR) { smatz@9015: DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR); rubidium@7213: } else { rubidium@7213: SetTileOwner(tile, new_player); rubidium@7213: } rubidium@7185: } rubidium@7157: return; truelight@0: } truelight@0: rubidium@7157: for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) { frosch@10689: /* Update all roadtypes, no matter if they are present */ rubidium@7157: if (GetRoadOwner(tile, rt) == old_player) { rubidium@7157: SetRoadOwner(tile, rt, new_player == PLAYER_SPECTATOR ? OWNER_NONE : new_player); rubidium@7157: } rubidium@7157: } tron@3060: rubidium@7157: if (IsLevelCrossing(tile)) { rubidium@7185: if (GetTileOwner(tile) == old_player) { rubidium@7185: if (new_player == PLAYER_SPECTATOR) { smatz@9094: DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL); rubidium@7185: } else { rubidium@7185: SetTileOwner(tile, new_player); rubidium@7185: } rubidium@7185: } truelight@0: } truelight@0: } truelight@0: rubidium@7990: static CommandCost TerraformTile_Road(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new) rubidium@7990: { rubidium@10775: if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) { rubidium@8078: switch (GetRoadTileType(tile)) { rubidium@8078: case ROAD_TILE_CROSSING: rubidium@8726: 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@8078: break; rubidium@8078: rubidium@8078: case ROAD_TILE_DEPOT: rubidium@8726: if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@8078: break; rubidium@8078: rubidium@8078: case ROAD_TILE_NORMAL: { rubidium@8078: RoadBits bits = GetAllRoadBits(tile); rubidium@8078: RoadBits bits_copy = bits; rubidium@8078: /* Check if the slope-road_bits combination is valid at all, i.e. it is save to call GetRoadFoundation(). */ skidd13@9240: if (!CmdFailed(CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE))) { rubidium@8078: /* CheckRoadSlope() sometimes changes the road_bits, if it does not agree with them. */ rubidium@8078: if (bits == bits_copy) { rubidium@8078: uint z_old; rubidium@8078: Slope tileh_old = GetTileSlope(tile, &z_old); rubidium@8078: rubidium@8078: /* Get the slope on top of the foundation */ rubidium@8078: z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old); rubidium@8078: z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new); rubidium@8078: rubidium@8078: /* The surface slope must not be changed */ rubidium@8726: if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@8078: } rubidium@8078: } rubidium@8078: break; rubidium@8078: } rubidium@8078: rubidium@8078: default: NOT_REACHED(); rubidium@8078: } rubidium@8078: } rubidium@8078: rubidium@7990: return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@7990: } rubidium@7990: truelight@0: rubidium@5838: 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@7831: GetFoundation_Road, /* get_foundation_proc */ rubidium@7990: TerraformTile_Road, /* terraform_tile_proc */ truelight@0: };