00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "cmd_helper.h"
00014 #include "road_internal.h"
00015 #include "landscape.h"
00016 #include "viewport_func.h"
00017 #include "command_func.h"
00018 #include "pathfinder/yapf/yapf_cache.h"
00019 #include "depot_base.h"
00020 #include "newgrf.h"
00021 #include "variables.h"
00022 #include "autoslope.h"
00023 #include "tunnelbridge_map.h"
00024 #include "window_func.h"
00025 #include "strings_func.h"
00026 #include "vehicle_func.h"
00027 #include "sound_func.h"
00028 #include "tunnelbridge.h"
00029 #include "cheat_type.h"
00030 #include "functions.h"
00031 #include "effectvehicle_func.h"
00032 #include "effectvehicle_base.h"
00033 #include "elrail_func.h"
00034 #include "roadveh.h"
00035 #include "town.h"
00036 #include "company_base.h"
00037 #include "core/random_func.hpp"
00038 #include "newgrf_railtype.h"
00039
00040 #include "table/strings.h"
00041
00046 bool RoadVehiclesAreBuilt()
00047 {
00048 const RoadVehicle *rv;
00049 FOR_ALL_ROADVEHICLES(rv) return true;
00050
00051 return false;
00052 }
00053
00054 #define M(x) (1 << (x))
00055
00056 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));
00057 #undef M
00058
00059
00060 static const RoadBits _invalid_tileh_slopes_road[2][15] = {
00061
00062 {
00063 ROAD_NONE,
00064 ROAD_NE | ROAD_SE,
00065 ROAD_NE | ROAD_NW,
00066
00067 ROAD_NE,
00068 ROAD_NW | ROAD_SW,
00069 ROAD_NONE,
00070
00071 ROAD_NW,
00072 ROAD_NONE,
00073 ROAD_SE | ROAD_SW,
00074
00075 ROAD_SE,
00076 ROAD_NONE,
00077 ROAD_NONE,
00078
00079 ROAD_SW,
00080 ROAD_NONE,
00081 ROAD_NONE
00082 },
00083
00084
00085 {
00086 ROAD_NONE,
00087 ROAD_NONE,
00088 ROAD_NONE,
00089
00090 ROAD_Y,
00091 ROAD_NONE,
00092 ROAD_ALL,
00093
00094 ROAD_X,
00095 ROAD_ALL,
00096 ROAD_NONE,
00097
00098 ROAD_X,
00099 ROAD_ALL,
00100 ROAD_ALL,
00101
00102 ROAD_Y,
00103 ROAD_ALL,
00104 ROAD_ALL
00105 }
00106 };
00107
00108 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits);
00109
00120 bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, RoadType rt, DoCommandFlag flags, bool town_check)
00121 {
00122 if (_game_mode == GM_EDITOR || remove == ROAD_NONE) return true;
00123
00124
00125
00126
00127 if (_current_company == OWNER_WATER ||
00128 (rt == ROADTYPE_ROAD && !Company::IsValidID(_current_company))) return true;
00129
00130
00131
00132 if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner);
00133
00134 if (!town_check) return true;
00135
00136 if (_cheats.magic_bulldozer.value) return true;
00137
00138 Town *t = ClosestTownFromTile(tile, UINT_MAX);
00139 if (t == NULL) return true;
00140
00141
00142
00143 if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return false;
00144
00145
00146 RoadBits n = ROAD_NONE;
00147 RoadBits present = GetAnyRoadBits(tile, rt);
00148 if ((present & ROAD_NE) && (GetAnyRoadBits(TILE_ADDXY(tile, -1, 0), rt) & ROAD_SW)) n |= ROAD_NE;
00149 if ((present & ROAD_SE) && (GetAnyRoadBits(TILE_ADDXY(tile, 0, 1), rt) & ROAD_NW)) n |= ROAD_SE;
00150 if ((present & ROAD_SW) && (GetAnyRoadBits(TILE_ADDXY(tile, 1, 0), rt) & ROAD_NE)) n |= ROAD_SW;
00151 if ((present & ROAD_NW) && (GetAnyRoadBits(TILE_ADDXY(tile, 0, -1), rt) & ROAD_SE)) n |= ROAD_NW;
00152
00153 int rating_decrease = RATING_ROAD_DOWN_STEP_EDGE;
00154
00155
00156 if (KillFirstBit(n) != ROAD_NONE && (n & remove) != ROAD_NONE) {
00157
00158 if (!_settings_game.construction.extra_dynamite) {
00159 SetDParam(0, t->index);
00160 _error_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS;
00161 return false;
00162 }
00163 rating_decrease = RATING_ROAD_DOWN_STEP_INNER;
00164 }
00165 ChangeTownRating(t, rating_decrease, RATING_ROAD_MINIMUM, flags);
00166
00167 return true;
00168 }
00169
00170
00179 static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits pieces, RoadType rt, bool crossing_check, bool town_check = true)
00180 {
00181 RoadTypes rts = GetRoadTypes(tile);
00182
00183 if (!HasBit(rts, rt)) return CMD_ERROR;
00184
00185 switch (GetTileType(tile)) {
00186 case MP_ROAD:
00187 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00188 break;
00189
00190 case MP_STATION:
00191 if (!IsDriveThroughStopTile(tile)) return CMD_ERROR;
00192 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00193 break;
00194
00195 case MP_TUNNELBRIDGE:
00196 if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR;
00197 if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
00198 break;
00199
00200 default:
00201 return CMD_ERROR;
00202 }
00203
00204 if (!CheckAllowRemoveRoad(tile, pieces, GetRoadOwner(tile, rt), rt, flags, town_check)) return CMD_ERROR;
00205
00206 if (!IsTileType(tile, MP_ROAD)) {
00207
00208 if (rts == RoadTypeToRoadTypes(rt)) return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00209
00210 CommandCost cost(EXPENSES_CONSTRUCTION);
00211 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00212 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
00213
00214 cost.AddCost((GetTunnelBridgeLength(other_end, tile) + 2) * _price[PR_CLEAR_ROAD]);
00215 if (flags & DC_EXEC) {
00216 SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt));
00217 SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
00218
00219
00220
00221 RoadType other_rt = (rt == ROADTYPE_ROAD) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
00222 Owner other_owner = GetRoadOwner(tile, other_rt);
00223 if (other_owner != GetTileOwner(tile)) {
00224 SetTileOwner(tile, other_owner);
00225 SetTileOwner(other_end, other_owner);
00226 }
00227
00228
00229 MarkTileDirtyByTile(tile);
00230 MarkTileDirtyByTile(other_end);
00231 if (IsBridge(tile)) {
00232 TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
00233
00234 for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
00235 }
00236 }
00237 } else {
00238 assert(IsDriveThroughStopTile(tile));
00239 cost.AddCost(_price[PR_CLEAR_ROAD] * 2);
00240 if (flags & DC_EXEC) {
00241 SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
00242 MarkTileDirtyByTile(tile);
00243 }
00244 }
00245 return cost;
00246 }
00247
00248 switch (GetRoadTileType(tile)) {
00249 case ROAD_TILE_NORMAL: {
00250 const Slope tileh = GetTileSlope(tile, NULL);
00251 RoadBits present = GetRoadBits(tile, rt);
00252 const RoadBits other = GetOtherRoadBits(tile, rt);
00253 const Foundation f = GetRoadFoundation(tileh, present);
00254
00255 if (HasRoadWorks(tile) && _current_company != OWNER_WATER) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
00256
00257
00258
00259
00260
00261 if (IsSteepSlope(tileh) || (IsStraightRoad(other) &&
00262 (other & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) != ROAD_NONE) ||
00263 (tileh != SLOPE_FLAT && !_settings_game.construction.build_on_slopes)) {
00264 pieces |= MirrorRoadBits(pieces);
00265 }
00266
00267
00268 pieces &= present;
00269 if (pieces == ROAD_NONE) return CMD_ERROR;
00270
00271
00272 present ^= pieces;
00273
00274
00275 if (tileh != SLOPE_FLAT && present != ROAD_NONE &&
00276 (present & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) == present) {
00277 return CMD_ERROR;
00278 }
00279
00280 if (flags & DC_EXEC) {
00281 if (HasRoadWorks(tile)) {
00282
00283 assert(_current_company == OWNER_WATER);
00284 EffectVehicle *v;
00285 FOR_ALL_EFFECTVEHICLES(v) {
00286 if (TileVirtXY(v->x_pos, v->y_pos) == tile) {
00287 delete v;
00288 }
00289 }
00290 }
00291 if (present == ROAD_NONE) {
00292 RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
00293 if (rts == ROADTYPES_NONE) {
00294
00295 DoClearSquare(tile);
00296 } else {
00297 if (rt == ROADTYPE_ROAD && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) {
00298
00299 const Town *town = CalcClosestTownFromTile(tile);
00300 SetTownIndex(tile, town == NULL ? (TownID)INVALID_TOWN : town->index);
00301 }
00302 SetRoadBits(tile, ROAD_NONE, rt);
00303 SetRoadTypes(tile, rts);
00304 MarkTileDirtyByTile(tile);
00305 }
00306 } else {
00307
00308
00309
00310 if (rt != ROADTYPE_TRAM) SetDisallowedRoadDirections(tile, DRD_NONE);
00311 SetRoadBits(tile, present, rt);
00312 MarkTileDirtyByTile(tile);
00313 }
00314 }
00315
00316 CommandCost cost(EXPENSES_CONSTRUCTION, CountBits(pieces) * _price[PR_CLEAR_ROAD]);
00317
00318 if (f == FOUNDATION_NONE && GetRoadFoundation(tileh, present) != FOUNDATION_NONE) cost.AddCost(_price[PR_BUILD_FOUNDATION]);
00319
00320 return cost;
00321 }
00322
00323 case ROAD_TILE_CROSSING: {
00324 if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) {
00325 return CMD_ERROR;
00326 }
00327
00328
00329
00330 if (rt == ROADTYPE_ROAD && HasTileRoadType(tile, ROADTYPE_TRAM) && (flags & DC_EXEC || crossing_check)) return CMD_ERROR;
00331
00332 if (flags & DC_EXEC) {
00333 Track railtrack = GetCrossingRailTrack(tile);
00334 RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
00335 if (rts == ROADTYPES_NONE) {
00336 TrackBits tracks = GetCrossingRailBits(tile);
00337 bool reserved = HasCrossingReservation(tile);
00338 MakeRailNormal(tile, GetTileOwner(tile), tracks, GetRailType(tile));
00339 if (reserved) SetTrackReservation(tile, tracks);
00340 } else {
00341 SetRoadTypes(tile, rts);
00342
00343 }
00344 MarkTileDirtyByTile(tile);
00345 YapfNotifyTrackLayoutChange(tile, railtrack);
00346 }
00347 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_ROAD] * 2);
00348 }
00349
00350 default:
00351 case ROAD_TILE_DEPOT:
00352 return CMD_ERROR;
00353 }
00354 }
00355
00356
00368 static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
00369 {
00370
00371 CLRBITS(*pieces, existing);
00372
00373
00374 if (*pieces == ROAD_NONE) return CMD_ERROR;
00375
00376
00377 if (tileh == SLOPE_FLAT) return CommandCost();
00378
00379
00380 if (IsSteepSlope(tileh)) {
00381
00382 *pieces |= MirrorRoadBits(*pieces);
00383
00384
00385
00386 existing |= other;
00387
00388 if ((existing == ROAD_NONE || existing == *pieces) && IsStraightRoad(*pieces)) {
00389 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00390 }
00391 return CMD_ERROR;
00392 }
00393
00394
00395 RoadBits type_bits = existing | *pieces;
00396
00397
00398 if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) {
00399
00400
00401 if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00402
00403 return CommandCost();
00404 }
00405
00406
00407 *pieces |= MirrorRoadBits(*pieces);
00408 type_bits = existing | *pieces;
00409
00410
00411 if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) &&
00412 (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) {
00413
00414
00415 if (IsSlopeWithOneCornerRaised(tileh)) {
00416
00417
00418 if (_settings_game.construction.build_on_slopes) {
00419
00420
00421 if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00422
00423 return CommandCost();
00424 }
00425 } else {
00426 if (HasExactlyOneBit(existing) && GetRoadFoundation(tileh, existing) == FOUNDATION_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00427 return CommandCost();
00428 }
00429 }
00430 return CMD_ERROR;
00431 }
00432
00443 CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00444 {
00445 CommandCost cost(EXPENSES_CONSTRUCTION);
00446
00447 RoadBits existing = ROAD_NONE;
00448 RoadBits other_bits = ROAD_NONE;
00449
00450
00451
00452 if ((Company::IsValidID(_current_company) && p2 != 0) || (_current_company == OWNER_TOWN && !Town::IsValidID(p2))) return CMD_ERROR;
00453 if (_current_company != OWNER_TOWN) {
00454 const Town *town = CalcClosestTownFromTile(tile);
00455 p2 = (town != NULL) ? town->index : (TownID)INVALID_TOWN;
00456 }
00457
00458 RoadBits pieces = Extract<RoadBits, 0>(p1);
00459
00460
00461 if (pieces == ROAD_NONE) return CMD_ERROR;
00462
00463 RoadType rt = (RoadType)GB(p1, 4, 2);
00464 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00465
00466 DisallowedRoadDirections toggle_drd = (DisallowedRoadDirections)GB(p1, 6, 2);
00467
00468 Slope tileh = GetTileSlope(tile, NULL);
00469
00470 bool tile_cleared = false;
00471 switch (GetTileType(tile)) {
00472 case MP_ROAD:
00473 switch (GetRoadTileType(tile)) {
00474 case ROAD_TILE_NORMAL: {
00475 if (HasRoadWorks(tile)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
00476
00477 other_bits = GetOtherRoadBits(tile, rt);
00478 if (!HasTileRoadType(tile, rt)) break;
00479
00480 existing = GetRoadBits(tile, rt);
00481 bool crossing = !IsStraightRoad(existing | pieces);
00482 if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) {
00483
00484 return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
00485 }
00486 if ((existing & pieces) == pieces) {
00487
00488 if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM) {
00489 if (crossing) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
00490
00491 Owner owner = GetRoadOwner(tile, ROADTYPE_ROAD);
00492 if (owner != OWNER_NONE && !CheckOwnership(owner, tile)) return CMD_ERROR;
00493
00494 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00495
00496
00497 if ((flags & DC_EXEC) && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) {
00498 SetDisallowedRoadDirections(tile, GetDisallowedRoadDirections(tile) ^ toggle_drd);
00499 MarkTileDirtyByTile(tile);
00500 }
00501 return CommandCost();
00502 }
00503 return_cmd_error(STR_ERROR_ALREADY_BUILT);
00504 }
00505 } break;
00506
00507 case ROAD_TILE_CROSSING:
00508 other_bits = GetCrossingRoadBits(tile);
00509 if (pieces & ComplementRoadBits(other_bits)) goto do_clear;
00510 pieces = other_bits;
00511
00512 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00513 break;
00514
00515 case ROAD_TILE_DEPOT:
00516 if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00517 goto do_clear;
00518
00519 default: NOT_REACHED();
00520 }
00521 break;
00522
00523 case MP_RAILWAY: {
00524 if (IsSteepSlope(tileh)) {
00525 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00526 }
00527
00528
00529 if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) {
00530 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00531 }
00532
00533 if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
00534
00535 Axis roaddir;
00536 switch (GetTrackBits(tile)) {
00537 case TRACK_BIT_X:
00538 if (pieces & ROAD_X) goto do_clear;
00539 roaddir = AXIS_Y;
00540 break;
00541
00542 case TRACK_BIT_Y:
00543 if (pieces & ROAD_Y) goto do_clear;
00544 roaddir = AXIS_X;
00545 break;
00546
00547 default: goto do_clear;
00548 }
00549
00550 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00551
00552 if (flags & DC_EXEC) {
00553 Track railtrack = AxisToTrack(OtherAxis(roaddir));
00554 YapfNotifyTrackLayoutChange(tile, railtrack);
00555
00556 bool reserved = HasBit(GetRailReservationTrackBits(tile), railtrack);
00557 MakeRoadCrossing(tile, _current_company, _current_company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
00558 SetCrossingReservation(tile, reserved);
00559 UpdateLevelCrossing(tile, false);
00560 MarkTileDirtyByTile(tile);
00561 }
00562 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_ROAD] * (rt == ROADTYPE_ROAD ? 2 : 4));
00563 }
00564
00565 case MP_STATION: {
00566 if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00567 if (!IsDriveThroughStopTile(tile)) goto do_clear;
00568
00569 RoadBits curbits = AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)));
00570 if (pieces & ~curbits) goto do_clear;
00571 pieces = curbits;
00572
00573 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00574 } break;
00575
00576 case MP_TUNNELBRIDGE:
00577 if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) goto do_clear;
00578 if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) goto do_clear;
00579 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00580
00581 if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
00582 break;
00583
00584 default: {
00585 do_clear:;
00586 CommandCost ret = DoCommand(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
00587 if (ret.Failed()) return ret;
00588 cost.AddCost(ret);
00589 tile_cleared = true;
00590 } break;
00591 }
00592
00593 if (other_bits != pieces) {
00594
00595 CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
00596
00597
00598 if (ret.Failed() || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
00599 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00600 }
00601 cost.AddCost(ret);
00602 }
00603
00604 if (!tile_cleared && IsTileType(tile, MP_ROAD)) {
00605
00606 pieces &= ComplementRoadBits(existing);
00607
00608
00609 if (IsNormalRoad(tile)) {
00610 Slope slope = GetTileSlope(tile, NULL);
00611 Foundation found_new = GetRoadFoundation(slope, pieces | existing);
00612
00613
00614 for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
00615 if (rtest != rt) {
00616 RoadBits bits = GetRoadBits(tile, rtest);
00617
00618 if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
00619 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00620 }
00621 }
00622 }
00623 }
00624 }
00625
00626 if (!tile_cleared && !EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00627
00628 cost.AddCost(CountBits(pieces) * _price[PR_BUILD_ROAD]);
00629 if (!tile_cleared && IsTileType(tile, MP_TUNNELBRIDGE)) {
00630
00631 cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
00632 }
00633
00634 if (flags & DC_EXEC) {
00635
00636
00637 if (tile_cleared) DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00638
00639 switch (GetTileType(tile)) {
00640 case MP_ROAD: {
00641 RoadTileType rtt = GetRoadTileType(tile);
00642 if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
00643 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00644 SetRoadOwner(tile, rt, _current_company);
00645 if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
00646 }
00647 if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
00648 } break;
00649
00650 case MP_TUNNELBRIDGE: {
00651 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
00652
00653 SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
00654 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00655 SetRoadOwner(other_end, rt, _current_company);
00656 SetRoadOwner(tile, rt, _current_company);
00657
00658
00659 MarkTileDirtyByTile(other_end);
00660 MarkTileDirtyByTile(tile);
00661 if (IsBridge(tile)) {
00662 TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
00663
00664 for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
00665 }
00666 } break;
00667
00668 case MP_STATION:
00669 assert(IsDriveThroughStopTile(tile));
00670 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00671 SetRoadOwner(tile, rt, _current_company);
00672 break;
00673
00674 default:
00675 MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_company, _current_company);
00676 break;
00677 }
00678
00679 if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
00680 existing |= pieces;
00681 SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
00682 GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
00683 }
00684
00685 MarkTileDirtyByTile(tile);
00686 }
00687 return cost;
00688 }
00689
00704 CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00705 {
00706 CommandCost cost(EXPENSES_CONSTRUCTION);
00707 bool had_bridge = false;
00708 bool had_tunnel = false;
00709 bool had_success = false;
00710 DisallowedRoadDirections drd = DRD_NORTHBOUND;
00711
00712 _error_message = INVALID_STRING_ID;
00713
00714 if (p1 >= MapSize()) return CMD_ERROR;
00715
00716 TileIndex end_tile = p1;
00717 RoadType rt = (RoadType)GB(p2, 3, 2);
00718 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00719
00720 Axis axis = Extract<Axis, 2>(p2);
00721
00722 if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR;
00723 if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR;
00724
00725 DiagDirection dir = AxisToDiagDir(axis);
00726
00727
00728 if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
00729 dir = ReverseDiagDir(dir);
00730 p2 ^= 3;
00731 drd = DRD_SOUTHBOUND;
00732 }
00733
00734
00735
00736
00737 if ((axis == AXIS_Y) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
00738
00739 if (!HasBit(p2, 5)) drd = DRD_NONE;
00740
00741 TileIndex tile = start_tile;
00742
00743 for (;;) {
00744 RoadBits bits = AxisToRoadBits(axis);
00745
00746
00747 if (tile == end_tile && !HasBit(p2, 1)) bits &= DiagDirToRoadBits(ReverseDiagDir(dir));
00748 if (tile == start_tile && HasBit(p2, 0)) bits &= DiagDirToRoadBits(dir);
00749
00750 _error_message = INVALID_STRING_ID;
00751 CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
00752 if (ret.Failed()) {
00753 if (_error_message != STR_ERROR_ALREADY_BUILT) {
00754 if (HasBit(p2, 6)) return CMD_ERROR;
00755 break;
00756 }
00757 } else {
00758 had_success = true;
00759
00760 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00761 if (IsBridge(tile)) {
00762 if ((!had_bridge || GetTunnelBridgeDirection(tile) == dir)) {
00763 cost.AddCost(ret);
00764 }
00765 had_bridge = true;
00766 } else {
00767 if ((!had_tunnel || GetTunnelBridgeDirection(tile) == dir)) {
00768 cost.AddCost(ret);
00769 }
00770 had_tunnel = true;
00771 }
00772 } else {
00773 cost.AddCost(ret);
00774 }
00775 }
00776
00777 if (tile == end_tile) break;
00778
00779 tile += TileOffsByDiagDir(dir);
00780 }
00781
00782 return !had_success ? CMD_ERROR : cost;
00783 }
00784
00797 CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00798 {
00799 CommandCost cost(EXPENSES_CONSTRUCTION);
00800
00801 if (p1 >= MapSize()) return CMD_ERROR;
00802
00803 TileIndex end_tile = p1;
00804 RoadType rt = (RoadType)GB(p2, 3, 2);
00805 if (!IsValidRoadType(rt)) return CMD_ERROR;
00806
00807 Axis axis = Extract<Axis, 2>(p2);
00808
00809 if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR;
00810 if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR;
00811
00812
00813 if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
00814 TileIndex t = start_tile;
00815 start_tile = end_tile;
00816 end_tile = t;
00817 p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
00818 }
00819
00820 Money money = GetAvailableMoneyForCommand();
00821 TileIndex tile = start_tile;
00822
00823 for (;;) {
00824 RoadBits bits = AxisToRoadBits(axis);
00825
00826 if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
00827 if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
00828
00829
00830 if (bits != 0) {
00831 CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
00832 if (ret.Succeeded()) {
00833 if (flags & DC_EXEC) {
00834 money -= ret.GetCost();
00835 if (money < 0) {
00836 _additional_cash_required = DoCommand(start_tile, end_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
00837 return cost;
00838 }
00839 RemoveRoad(tile, flags, bits, rt, true, false);
00840 }
00841 cost.AddCost(ret);
00842 }
00843 }
00844
00845 if (tile == end_tile) break;
00846
00847 tile += (axis == AXIS_Y) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
00848 }
00849
00850 return (cost.GetCost() == 0) ? CMD_ERROR : cost;
00851 }
00852
00865 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00866 {
00867 DiagDirection dir = Extract<DiagDirection, 0>(p1);
00868 RoadType rt = (RoadType)GB(p1, 2, 2);
00869
00870 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00871
00872 Slope tileh = GetTileSlope(tile, NULL);
00873 if (tileh != SLOPE_FLAT && (
00874 !_settings_game.construction.build_on_slopes ||
00875 IsSteepSlope(tileh) ||
00876 !CanBuildDepotByTileh(dir, tileh)
00877 )) {
00878 return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
00879 }
00880
00881 CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00882 if (cost.Failed()) return CMD_ERROR;
00883
00884 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00885
00886 if (!Depot::CanAllocateItem()) return CMD_ERROR;
00887
00888 if (flags & DC_EXEC) {
00889 Depot *dep = new Depot(tile);
00890 dep->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
00891
00892 MakeRoadDepot(tile, _current_company, dep->index, dir, rt);
00893 MarkTileDirtyByTile(tile);
00894 }
00895 cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
00896 return cost;
00897 }
00898
00899 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
00900 {
00901 if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
00902
00903 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00904
00905 if (flags & DC_EXEC) {
00906 delete Depot::GetByTile(tile);
00907 DoClearSquare(tile);
00908 }
00909
00910 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_ROAD]);
00911 }
00912
00913 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
00914 {
00915 switch (GetRoadTileType(tile)) {
00916 case ROAD_TILE_NORMAL: {
00917 RoadBits b = GetAllRoadBits(tile);
00918
00919
00920 if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
00921 RoadTypes rts = GetRoadTypes(tile);
00922 CommandCost ret(EXPENSES_CONSTRUCTION);
00923 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
00924 if (HasBit(rts, rt)) {
00925 CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
00926 if (tmp_ret.Failed()) return tmp_ret;
00927 ret.AddCost(tmp_ret);
00928 }
00929 }
00930 return ret;
00931 }
00932 return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
00933 }
00934
00935 case ROAD_TILE_CROSSING: {
00936 RoadTypes rts = GetRoadTypes(tile);
00937 CommandCost ret(EXPENSES_CONSTRUCTION);
00938
00939 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
00940
00941
00942
00943 RoadType rt = ROADTYPE_TRAM;
00944 do {
00945 if (HasBit(rts, rt)) {
00946 CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
00947 if (tmp_ret.Failed()) return tmp_ret;
00948 ret.AddCost(tmp_ret);
00949 }
00950 } while (rt-- != ROADTYPE_ROAD);
00951
00952 if (flags & DC_EXEC) {
00953 DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00954 }
00955 return ret;
00956 }
00957
00958 default:
00959 case ROAD_TILE_DEPOT:
00960 if (flags & DC_AUTO) {
00961 return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
00962 }
00963 return RemoveRoadDepot(tile, flags);
00964 }
00965 }
00966
00967
00968 struct DrawRoadTileStruct {
00969 uint16 image;
00970 byte subcoord_x;
00971 byte subcoord_y;
00972 };
00973
00974 #include "table/road_land.h"
00975
00983 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
00984 {
00985
00986 if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
00987
00988 if (!IsSteepSlope(tileh)) {
00989
00990 if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
00991
00992
00993 if (!IsSlopeWithOneCornerRaised(tileh) &&
00994 (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
00995 return FOUNDATION_NONE;
00996 }
00997
00998
00999 return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
01000 }
01001
01002 const byte _road_sloped_sprites[14] = {
01003 0, 0, 2, 0,
01004 0, 1, 0, 0,
01005 3, 0, 0, 0,
01006 0, 0
01007 };
01008
01019 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
01020 {
01021 return (IsOnSnow(tile) &&
01022 !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
01023 roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
01024 }
01025
01031 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
01032 {
01033
01034 if (IsInvisibilitySet(TO_CATENARY)) return;
01035
01036
01037 if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
01038 uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
01039
01040 if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
01041 }
01042
01043 SpriteID front;
01044 SpriteID back;
01045
01046 if (ti->tileh != SLOPE_FLAT) {
01047 back = SPR_TRAMWAY_BACK_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
01048 front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
01049 } else {
01050 back = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
01051 front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
01052 }
01053
01054 AddSortableSpriteToDraw(back, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
01055 AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
01056 }
01057
01066 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
01067 {
01068 int x = ti->x | dx;
01069 int y = ti->y | dy;
01070 byte z = ti->z;
01071 if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
01072 AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
01073 }
01074
01079 static void DrawRoadBits(TileInfo *ti)
01080 {
01081 RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
01082 RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
01083
01084 SpriteID image = 0;
01085 PaletteID pal = PAL_NONE;
01086
01087 if (ti->tileh != SLOPE_FLAT) {
01088 DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
01089
01090
01091
01092 if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
01093 }
01094
01095 if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
01096
01097 Roadside roadside = GetRoadside(ti->tile);
01098
01099 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01100 image += 19;
01101 } else {
01102 switch (roadside) {
01103 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01104 case ROADSIDE_GRASS: break;
01105 case ROADSIDE_GRASS_ROAD_WORKS: break;
01106 default: image -= 19; break;
01107 }
01108 }
01109
01110 DrawGroundSprite(image, pal);
01111
01112
01113
01114
01115 if (tram != ROAD_NONE) {
01116 if (ti->tileh != SLOPE_FLAT) {
01117 image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
01118 } else {
01119 image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
01120 }
01121 image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
01122 DrawGroundSprite(image, pal);
01123 }
01124
01125 if (road != ROAD_NONE) {
01126 DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
01127 if (drd != DRD_NONE) {
01128 DrawGroundSpriteAt(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), PAL_NONE, 8, 8, GetPartialZ(8, 8, ti->tileh));
01129 }
01130 }
01131
01132 if (HasRoadWorks(ti->tile)) {
01133
01134 DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
01135 return;
01136 }
01137
01138 if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
01139
01140
01141 if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
01142
01143
01144 if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
01145 uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
01146 uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT;
01147
01148 if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT;
01149
01150 if (height < minz) return;
01151 }
01152
01153
01154 if (HasAtMostOneBit(road)) return;
01155
01156
01157 for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
01158 DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
01159 }
01160 }
01161
01163 static void DrawTile_Road(TileInfo *ti)
01164 {
01165 switch (GetRoadTileType(ti->tile)) {
01166 case ROAD_TILE_NORMAL:
01167 DrawRoadBits(ti);
01168 break;
01169
01170 case ROAD_TILE_CROSSING: {
01171 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
01172
01173 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01174
01175 if (rti->UsesOverlay()) {
01176 Axis axis = GetCrossingRailAxis(ti->tile);
01177 SpriteID road = SPR_ROAD_Y + axis;
01178 PaletteID pal = PAL_NONE;
01179
01180 Roadside roadside = GetRoadside(ti->tile);
01181
01182 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01183 road += 19;
01184 } else {
01185 switch (roadside) {
01186 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01187 case ROADSIDE_GRASS: break;
01188 default: road -= 19; break;
01189 }
01190 }
01191
01192 DrawGroundSprite(road, pal);
01193
01194 SpriteID rail = GetCustomRailSprite(rti, ti->tile, RTSG_CROSSING) + axis;
01195 DrawGroundSprite(rail, PAL_NONE);
01196 DrawRailTileSeq(ti, &_crossing_layout, TO_CATENARY, rail, 0, PAL_NONE);
01197
01198 if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
01199 break;
01200 }
01201
01202 SpriteID image = rti->base_sprites.crossing;
01203 PaletteID pal = PAL_NONE;
01204
01205 if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
01206 if (IsCrossingBarred(ti->tile)) image += 2;
01207
01208 Roadside roadside = GetRoadside(ti->tile);
01209
01210 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01211 image += 8;
01212 } else {
01213 switch (roadside) {
01214 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01215 case ROADSIDE_GRASS: break;
01216 default: image += 4; break;
01217 }
01218 }
01219
01220 DrawGroundSprite(image, pal);
01221
01222
01223 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) {
01224 DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y, PALETTE_CRASH);
01225 }
01226
01227 if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
01228 DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
01229 DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile));
01230 }
01231 if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
01232 break;
01233 }
01234
01235 default:
01236 case ROAD_TILE_DEPOT: {
01237 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
01238
01239 PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
01240
01241 const DrawTileSprites *dts;
01242 if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
01243 dts = &_tram_depot[GetRoadDepotDirection(ti->tile)];
01244 } else {
01245 dts = &_road_depot[GetRoadDepotDirection(ti->tile)];
01246 }
01247
01248 DrawGroundSprite(dts->ground.sprite, PAL_NONE);
01249 DrawOrigTileSeq(ti, dts, TO_BUILDINGS, palette);
01250 break;
01251 }
01252 }
01253 DrawBridgeMiddle(ti);
01254 }
01255
01256 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
01257 {
01258 PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
01259 const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
01260
01261 x += 33;
01262 y += 17;
01263
01264 DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
01265 DrawOrigTileSeqInGUI(x, y, dts, palette);
01266 }
01267
01273 void UpdateNearestTownForRoadTiles(bool invalidate)
01274 {
01275 assert(!invalidate || _generating_world);
01276
01277 for (TileIndex t = 0; t < MapSize(); t++) {
01278 if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && !HasTownOwnedRoad(t)) {
01279 TownID tid = (TownID)INVALID_TOWN;
01280 if (!invalidate) {
01281 const Town *town = CalcClosestTownFromTile(t);
01282 if (town != NULL) tid = town->index;
01283 }
01284 SetTownIndex(t, tid);
01285 }
01286 }
01287 }
01288
01289 static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
01290 {
01291 uint z;
01292 Slope tileh = GetTileSlope(tile, &z);
01293
01294 if (tileh == SLOPE_FLAT) return z;
01295 if (IsNormalRoad(tile)) {
01296 Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
01297 z += ApplyFoundationToSlope(f, &tileh);
01298 return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
01299 } else {
01300 return z + TILE_HEIGHT;
01301 }
01302 }
01303
01304 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
01305 {
01306 if (IsNormalRoad(tile)) {
01307 return GetRoadFoundation(tileh, GetAllRoadBits(tile));
01308 } else {
01309 return FlatteningFoundation(tileh);
01310 }
01311 }
01312
01313 static const Roadside _town_road_types[][2] = {
01314 { ROADSIDE_GRASS, ROADSIDE_GRASS },
01315 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01316 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01317 { ROADSIDE_TREES, ROADSIDE_TREES },
01318 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
01319 };
01320
01321 static const Roadside _town_road_types_2[][2] = {
01322 { ROADSIDE_GRASS, ROADSIDE_GRASS },
01323 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01324 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
01325 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
01326 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
01327 };
01328
01329
01330 static void TileLoop_Road(TileIndex tile)
01331 {
01332 switch (_settings_game.game_creation.landscape) {
01333 case LT_ARCTIC:
01334 if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
01335 ToggleSnow(tile);
01336 MarkTileDirtyByTile(tile);
01337 }
01338 break;
01339
01340 case LT_TROPIC:
01341 if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
01342 ToggleDesert(tile);
01343 MarkTileDirtyByTile(tile);
01344 }
01345 break;
01346 }
01347
01348 if (IsRoadDepot(tile)) return;
01349
01350 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
01351 if (!HasRoadWorks(tile)) {
01352 HouseZonesBits grp = HZB_TOWN_EDGE;
01353
01354 if (t != NULL) {
01355 grp = GetTownRadiusGroup(t, tile);
01356
01357
01358 if (t->road_build_months != 0 &&
01359 (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
01360 IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) {
01361 if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) {
01362 StartRoadWorks(tile);
01363
01364 SndPlayTileFx(SND_21_JACKHAMMER, tile);
01365 CreateEffectVehicleAbove(
01366 TileX(tile) * TILE_SIZE + 7,
01367 TileY(tile) * TILE_SIZE + 7,
01368 0,
01369 EV_BULLDOZER);
01370 MarkTileDirtyByTile(tile);
01371 return;
01372 }
01373 }
01374 }
01375
01376 {
01377
01378 const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
01379 Roadside cur_rs = GetRoadside(tile);
01380
01381
01382 if (cur_rs == new_rs[0]) return;
01383
01384
01385 if (cur_rs == new_rs[1]) {
01386 cur_rs = new_rs[0];
01387
01388 } else if (cur_rs == ROADSIDE_BARREN) {
01389 cur_rs = new_rs[1];
01390
01391 } else {
01392 cur_rs = ROADSIDE_BARREN;
01393 }
01394 SetRoadside(tile, cur_rs);
01395 MarkTileDirtyByTile(tile);
01396 }
01397 } else if (IncreaseRoadWorksCounter(tile)) {
01398 TerminateRoadWorks(tile);
01399
01400 if (_settings_game.economy.mod_road_rebuild) {
01401
01402 const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
01403 const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
01404
01405 if (old_rb != new_rb) {
01406 RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true);
01407 }
01408 }
01409
01410 MarkTileDirtyByTile(tile);
01411 }
01412 }
01413
01414 static bool ClickTile_Road(TileIndex tile)
01415 {
01416 if (!IsRoadDepot(tile)) return false;
01417
01418 ShowDepotWindow(tile, VEH_ROAD);
01419 return true;
01420 }
01421
01422
01423 static const byte _road_trackbits[16] = {
01424 0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F,
01425 };
01426
01427 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
01428 {
01429 TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
01430 TrackdirBits red_signals = TRACKDIR_BIT_NONE;
01431 switch (mode) {
01432 case TRANSPORT_RAIL:
01433 if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
01434 break;
01435
01436 case TRANSPORT_ROAD:
01437 if ((GetRoadTypes(tile) & sub_mode) == 0) break;
01438 switch (GetRoadTileType(tile)) {
01439 case ROAD_TILE_NORMAL: {
01440 const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
01441 RoadType rt = (RoadType)FindFirstBit(sub_mode);
01442 RoadBits bits = GetRoadBits(tile, rt);
01443
01444
01445 if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
01446
01447 uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
01448 if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
01449 break;
01450 }
01451
01452 case ROAD_TILE_CROSSING: {
01453 Axis axis = GetCrossingRoadAxis(tile);
01454
01455 if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
01456
01457 trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
01458 if (IsCrossingBarred(tile)) red_signals = trackdirbits;
01459 break;
01460 }
01461
01462 default:
01463 case ROAD_TILE_DEPOT: {
01464 DiagDirection dir = GetRoadDepotDirection(tile);
01465
01466 if (side != INVALID_DIAGDIR && side != dir) break;
01467
01468 trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
01469 break;
01470 }
01471 }
01472 break;
01473
01474 default: break;
01475 }
01476 return CombineTrackStatus(trackdirbits, red_signals);
01477 }
01478
01479 static const StringID _road_tile_strings[] = {
01480 STR_LAI_ROAD_DESCRIPTION_ROAD,
01481 STR_LAI_ROAD_DESCRIPTION_ROAD,
01482 STR_LAI_ROAD_DESCRIPTION_ROAD,
01483 STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS,
01484 STR_LAI_ROAD_DESCRIPTION_ROAD,
01485 STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD,
01486 STR_LAI_ROAD_DESCRIPTION_ROAD,
01487 STR_LAI_ROAD_DESCRIPTION_ROAD,
01488 };
01489
01490 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
01491 {
01492 Owner rail_owner = INVALID_OWNER;
01493 Owner road_owner = INVALID_OWNER;
01494 Owner tram_owner = INVALID_OWNER;
01495
01496 switch (GetRoadTileType(tile)) {
01497 case ROAD_TILE_CROSSING: {
01498 td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING;
01499 RoadTypes rts = GetRoadTypes(tile);
01500 rail_owner = GetTileOwner(tile);
01501 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01502 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01503 break;
01504 }
01505
01506 case ROAD_TILE_DEPOT:
01507 td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT;
01508 road_owner = GetTileOwner(tile);
01509 break;
01510
01511 default: {
01512 RoadTypes rts = GetRoadTypes(tile);
01513 td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY);
01514 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01515 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01516 break;
01517 }
01518 }
01519
01520
01521
01522
01523
01524
01525 Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
01526 bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
01527
01528 if (mixed_owners) {
01529
01530 td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_RAIL_OWNER);
01531 td->owner[0] = rail_owner;
01532 td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_ROAD_OWNER);
01533 td->owner[1] = road_owner;
01534 td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_TRAM_OWNER);
01535 td->owner[2] = tram_owner;
01536 } else {
01537
01538 td->owner[0] = first_owner;
01539 }
01540 }
01541
01546 static const byte _roadveh_enter_depot_dir[4] = {
01547 TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
01548 };
01549
01550 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
01551 {
01552 switch (GetRoadTileType(tile)) {
01553 case ROAD_TILE_DEPOT: {
01554 if (v->type != VEH_ROAD) break;
01555
01556 RoadVehicle *rv = RoadVehicle::From(v);
01557 if (rv->frame == RVC_DEPOT_STOP_FRAME &&
01558 _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) {
01559 rv->state = RVSB_IN_DEPOT;
01560 rv->vehstatus |= VS_HIDDEN;
01561 rv->direction = ReverseDir(rv->direction);
01562 if (rv->Next() == NULL) VehicleEnterDepot(rv->First());
01563 rv->tile = tile;
01564
01565 InvalidateWindowData(WC_VEHICLE_DEPOT, rv->tile);
01566 return VETSB_ENTERED_WORMHOLE;
01567 }
01568 } break;
01569
01570 default: break;
01571 }
01572 return VETSB_CONTINUE;
01573 }
01574
01575
01576 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
01577 {
01578 if (IsRoadDepot(tile)) {
01579 if (GetTileOwner(tile) == old_owner) {
01580 if (new_owner == INVALID_OWNER) {
01581 DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
01582 } else {
01583 SetTileOwner(tile, new_owner);
01584 }
01585 }
01586 return;
01587 }
01588
01589 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
01590
01591 if (GetRoadOwner(tile, rt) == old_owner) {
01592 SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
01593 }
01594 }
01595
01596 if (IsLevelCrossing(tile)) {
01597 if (GetTileOwner(tile) == old_owner) {
01598 if (new_owner == INVALID_OWNER) {
01599 DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
01600 } else {
01601 SetTileOwner(tile, new_owner);
01602 }
01603 }
01604 }
01605 }
01606
01607 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
01608 {
01609 if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
01610 switch (GetRoadTileType(tile)) {
01611 case ROAD_TILE_CROSSING:
01612 if (!IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new)) && HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01613 break;
01614
01615 case ROAD_TILE_DEPOT:
01616 if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01617 break;
01618
01619 case ROAD_TILE_NORMAL: {
01620 RoadBits bits = GetAllRoadBits(tile);
01621 RoadBits bits_copy = bits;
01622
01623 if (CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE).Succeeded()) {
01624
01625 if (bits == bits_copy) {
01626 uint z_old;
01627 Slope tileh_old = GetTileSlope(tile, &z_old);
01628
01629
01630 z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
01631 z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
01632
01633
01634 if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01635 }
01636 }
01637 break;
01638 }
01639
01640 default: NOT_REACHED();
01641 }
01642 }
01643
01644 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
01645 }
01646
01648 extern const TileTypeProcs _tile_type_road_procs = {
01649 DrawTile_Road,
01650 GetSlopeZ_Road,
01651 ClearTile_Road,
01652 NULL,
01653 GetTileDesc_Road,
01654 GetTileTrackStatus_Road,
01655 ClickTile_Road,
01656 NULL,
01657 TileLoop_Road,
01658 ChangeTileOwner_Road,
01659 NULL,
01660 VehicleEnter_Road,
01661 GetFoundation_Road,
01662 TerraformTile_Road,
01663 };