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, 4>(p1);
00459
00460
00461 if (pieces == ROAD_NONE) return CMD_ERROR;
00462
00463 RoadType rt = Extract<RoadType, 4, 2>(p1);
00464 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00465
00466 DisallowedRoadDirections toggle_drd = Extract<DisallowedRoadDirections, 6, 2>(p1);
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 DisallowedRoadDirections dis_existing = GetDisallowedRoadDirections(tile);
00495 DisallowedRoadDirections dis_new = dis_existing ^ toggle_drd;
00496
00497
00498
00499
00500
00501 if (CountBits(dis_existing) <= CountBits(dis_new)) {
00502 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00503 }
00504
00505
00506 if ((flags & DC_EXEC) && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) {
00507 SetDisallowedRoadDirections(tile, dis_new);
00508 MarkTileDirtyByTile(tile);
00509 }
00510 return CommandCost();
00511 }
00512 return_cmd_error(STR_ERROR_ALREADY_BUILT);
00513 }
00514 } break;
00515
00516 case ROAD_TILE_CROSSING:
00517 other_bits = GetCrossingRoadBits(tile);
00518 if (pieces & ComplementRoadBits(other_bits)) goto do_clear;
00519 pieces = other_bits;
00520
00521 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00522 break;
00523
00524 case ROAD_TILE_DEPOT:
00525 if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00526 goto do_clear;
00527
00528 default: NOT_REACHED();
00529 }
00530 break;
00531
00532 case MP_RAILWAY: {
00533 if (IsSteepSlope(tileh)) {
00534 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00535 }
00536
00537
00538 if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) {
00539 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00540 }
00541
00542 if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
00543
00544 if (RailNoLevelCrossings(GetRailType(tile))) {
00545 return_cmd_error(STR_ERROR_CROSSING_DISALLOWED);
00546 }
00547
00548 Axis roaddir;
00549 switch (GetTrackBits(tile)) {
00550 case TRACK_BIT_X:
00551 if (pieces & ROAD_X) goto do_clear;
00552 roaddir = AXIS_Y;
00553 break;
00554
00555 case TRACK_BIT_Y:
00556 if (pieces & ROAD_Y) goto do_clear;
00557 roaddir = AXIS_X;
00558 break;
00559
00560 default: goto do_clear;
00561 }
00562
00563 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00564
00565 if (flags & DC_EXEC) {
00566 Track railtrack = AxisToTrack(OtherAxis(roaddir));
00567 YapfNotifyTrackLayoutChange(tile, railtrack);
00568
00569 bool reserved = HasBit(GetRailReservationTrackBits(tile), railtrack);
00570 MakeRoadCrossing(tile, _current_company, _current_company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
00571 SetCrossingReservation(tile, reserved);
00572 UpdateLevelCrossing(tile, false);
00573 MarkTileDirtyByTile(tile);
00574 }
00575 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_ROAD] * (rt == ROADTYPE_ROAD ? 2 : 4));
00576 }
00577
00578 case MP_STATION: {
00579 if ((GetAnyRoadBits(tile, rt) & pieces) == pieces) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00580 if (!IsDriveThroughStopTile(tile)) goto do_clear;
00581
00582 RoadBits curbits = AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)));
00583 if (pieces & ~curbits) goto do_clear;
00584 pieces = curbits;
00585
00586 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00587 } break;
00588
00589 case MP_TUNNELBRIDGE:
00590 if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) goto do_clear;
00591 if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) goto do_clear;
00592 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_ERROR_ALREADY_BUILT);
00593
00594 if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
00595 break;
00596
00597 default: {
00598 do_clear:;
00599 CommandCost ret = DoCommand(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR);
00600 if (ret.Failed()) return ret;
00601 cost.AddCost(ret);
00602 tile_cleared = true;
00603 } break;
00604 }
00605
00606 if (other_bits != pieces) {
00607
00608 CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
00609
00610
00611 if (ret.Failed() || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
00612 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00613 }
00614 cost.AddCost(ret);
00615 }
00616
00617 if (!tile_cleared && IsTileType(tile, MP_ROAD)) {
00618
00619 pieces &= ComplementRoadBits(existing);
00620
00621
00622 if (IsNormalRoad(tile)) {
00623 Slope slope = GetTileSlope(tile, NULL);
00624 Foundation found_new = GetRoadFoundation(slope, pieces | existing);
00625
00626
00627 for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
00628 if (rtest != rt) {
00629 RoadBits bits = GetRoadBits(tile, rtest);
00630
00631 if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
00632 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00633 }
00634 }
00635 }
00636 }
00637 }
00638
00639 if (!tile_cleared && !EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00640
00641 cost.AddCost(CountBits(pieces) * _price[PR_BUILD_ROAD]);
00642 if (!tile_cleared && IsTileType(tile, MP_TUNNELBRIDGE)) {
00643
00644 cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
00645 }
00646
00647 if (flags & DC_EXEC) {
00648
00649
00650 if (tile_cleared) DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00651
00652 switch (GetTileType(tile)) {
00653 case MP_ROAD: {
00654 RoadTileType rtt = GetRoadTileType(tile);
00655 if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
00656 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00657 SetRoadOwner(tile, rt, _current_company);
00658 if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
00659 }
00660 if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
00661 } break;
00662
00663 case MP_TUNNELBRIDGE: {
00664 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
00665
00666 SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
00667 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00668 SetRoadOwner(other_end, rt, _current_company);
00669 SetRoadOwner(tile, rt, _current_company);
00670
00671
00672 MarkTileDirtyByTile(other_end);
00673 MarkTileDirtyByTile(tile);
00674 if (IsBridge(tile)) {
00675 TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
00676
00677 for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
00678 }
00679 } break;
00680
00681 case MP_STATION:
00682 assert(IsDriveThroughStopTile(tile));
00683 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00684 SetRoadOwner(tile, rt, _current_company);
00685 break;
00686
00687 default:
00688 MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_company, _current_company);
00689 break;
00690 }
00691
00692 if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
00693 existing |= pieces;
00694 SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
00695 GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
00696 }
00697
00698 MarkTileDirtyByTile(tile);
00699 }
00700 return cost;
00701 }
00702
00717 CommandCost CmdBuildLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00718 {
00719 CommandCost cost(EXPENSES_CONSTRUCTION);
00720 bool had_bridge = false;
00721 bool had_tunnel = false;
00722 bool had_success = false;
00723 DisallowedRoadDirections drd = DRD_NORTHBOUND;
00724
00725 _error_message = INVALID_STRING_ID;
00726
00727 if (p1 >= MapSize()) return CMD_ERROR;
00728
00729 TileIndex end_tile = p1;
00730 RoadType rt = Extract<RoadType, 3, 2>(p2);
00731 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00732
00733 Axis axis = Extract<Axis, 2, 1>(p2);
00734
00735 if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR;
00736 if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR;
00737
00738 DiagDirection dir = AxisToDiagDir(axis);
00739
00740
00741 if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
00742 dir = ReverseDiagDir(dir);
00743 p2 ^= 3;
00744 drd = DRD_SOUTHBOUND;
00745 }
00746
00747
00748
00749
00750 if ((axis == AXIS_Y) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
00751
00752 if (!HasBit(p2, 5)) drd = DRD_NONE;
00753
00754 TileIndex tile = start_tile;
00755
00756 for (;;) {
00757 RoadBits bits = AxisToRoadBits(axis);
00758
00759
00760 if (tile == end_tile && !HasBit(p2, 1)) bits &= DiagDirToRoadBits(ReverseDiagDir(dir));
00761 if (tile == start_tile && HasBit(p2, 0)) bits &= DiagDirToRoadBits(dir);
00762
00763 _error_message = INVALID_STRING_ID;
00764 CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
00765 if (ret.Failed()) {
00766 if (_error_message != STR_ERROR_ALREADY_BUILT) {
00767 if (HasBit(p2, 6)) return CMD_ERROR;
00768 break;
00769 }
00770 } else {
00771 had_success = true;
00772
00773 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00774 if (IsBridge(tile)) {
00775 if ((!had_bridge || GetTunnelBridgeDirection(tile) == dir)) {
00776 cost.AddCost(ret);
00777 }
00778 had_bridge = true;
00779 } else {
00780 if ((!had_tunnel || GetTunnelBridgeDirection(tile) == dir)) {
00781 cost.AddCost(ret);
00782 }
00783 had_tunnel = true;
00784 }
00785 } else {
00786 cost.AddCost(ret);
00787 }
00788 }
00789
00790 if (tile == end_tile) break;
00791
00792 tile += TileOffsByDiagDir(dir);
00793 }
00794
00795 return !had_success ? CMD_ERROR : cost;
00796 }
00797
00810 CommandCost CmdRemoveLongRoad(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00811 {
00812 CommandCost cost(EXPENSES_CONSTRUCTION);
00813
00814 if (p1 >= MapSize()) return CMD_ERROR;
00815
00816 TileIndex end_tile = p1;
00817 RoadType rt = Extract<RoadType, 3, 2>(p2);
00818 if (!IsValidRoadType(rt)) return CMD_ERROR;
00819
00820 Axis axis = Extract<Axis, 2, 1>(p2);
00821
00822 if (axis == AXIS_X && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR;
00823 if (axis == AXIS_Y && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR;
00824
00825
00826 if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
00827 TileIndex t = start_tile;
00828 start_tile = end_tile;
00829 end_tile = t;
00830 p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
00831 }
00832
00833 Money money = GetAvailableMoneyForCommand();
00834 TileIndex tile = start_tile;
00835
00836 for (;;) {
00837 RoadBits bits = AxisToRoadBits(axis);
00838
00839 if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
00840 if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
00841
00842
00843 if (bits != 0) {
00844 CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
00845 if (ret.Succeeded()) {
00846 if (flags & DC_EXEC) {
00847 money -= ret.GetCost();
00848 if (money < 0) {
00849 _additional_cash_required = DoCommand(start_tile, end_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
00850 return cost;
00851 }
00852 RemoveRoad(tile, flags, bits, rt, true, false);
00853 }
00854 cost.AddCost(ret);
00855 }
00856 }
00857
00858 if (tile == end_tile) break;
00859
00860 tile += (axis == AXIS_Y) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
00861 }
00862
00863 return (cost.GetCost() == 0) ? CMD_ERROR : cost;
00864 }
00865
00878 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00879 {
00880 DiagDirection dir = Extract<DiagDirection, 0, 2>(p1);
00881 RoadType rt = Extract<RoadType, 2, 2>(p1);
00882
00883 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00884
00885 Slope tileh = GetTileSlope(tile, NULL);
00886 if (tileh != SLOPE_FLAT && (
00887 !_settings_game.construction.build_on_slopes ||
00888 IsSteepSlope(tileh) ||
00889 !CanBuildDepotByTileh(dir, tileh)
00890 )) {
00891 return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
00892 }
00893
00894 CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00895 if (cost.Failed()) return CMD_ERROR;
00896
00897 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00898
00899 if (!Depot::CanAllocateItem()) return CMD_ERROR;
00900
00901 if (flags & DC_EXEC) {
00902 Depot *dep = new Depot(tile);
00903 dep->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
00904
00905 MakeRoadDepot(tile, _current_company, dep->index, dir, rt);
00906 MarkTileDirtyByTile(tile);
00907 }
00908 cost.AddCost(_price[PR_BUILD_DEPOT_ROAD]);
00909 return cost;
00910 }
00911
00912 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
00913 {
00914 if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
00915
00916 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00917
00918 if (flags & DC_EXEC) {
00919 delete Depot::GetByTile(tile);
00920 DoClearSquare(tile);
00921 }
00922
00923 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_ROAD]);
00924 }
00925
00926 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
00927 {
00928 switch (GetRoadTileType(tile)) {
00929 case ROAD_TILE_NORMAL: {
00930 RoadBits b = GetAllRoadBits(tile);
00931
00932
00933 if ((HasExactlyOneBit(b) && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
00934 RoadTypes rts = GetRoadTypes(tile);
00935 CommandCost ret(EXPENSES_CONSTRUCTION);
00936 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
00937 if (HasBit(rts, rt)) {
00938 CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
00939 if (tmp_ret.Failed()) return tmp_ret;
00940 ret.AddCost(tmp_ret);
00941 }
00942 }
00943 return ret;
00944 }
00945 return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
00946 }
00947
00948 case ROAD_TILE_CROSSING: {
00949 RoadTypes rts = GetRoadTypes(tile);
00950 CommandCost ret(EXPENSES_CONSTRUCTION);
00951
00952 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_REMOVE_ROAD_FIRST);
00953
00954
00955
00956 RoadType rt = ROADTYPE_TRAM;
00957 do {
00958 if (HasBit(rts, rt)) {
00959 CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
00960 if (tmp_ret.Failed()) return tmp_ret;
00961 ret.AddCost(tmp_ret);
00962 }
00963 } while (rt-- != ROADTYPE_ROAD);
00964
00965 if (flags & DC_EXEC) {
00966 DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00967 }
00968 return ret;
00969 }
00970
00971 default:
00972 case ROAD_TILE_DEPOT:
00973 if (flags & DC_AUTO) {
00974 return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
00975 }
00976 return RemoveRoadDepot(tile, flags);
00977 }
00978 }
00979
00980
00981 struct DrawRoadTileStruct {
00982 uint16 image;
00983 byte subcoord_x;
00984 byte subcoord_y;
00985 };
00986
00987 #include "table/road_land.h"
00988
00996 static Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
00997 {
00998
00999 if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
01000
01001 if (!IsSteepSlope(tileh)) {
01002
01003 if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
01004
01005
01006 if (!IsSlopeWithOneCornerRaised(tileh) &&
01007 (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
01008 return FOUNDATION_NONE;
01009 }
01010
01011
01012 return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
01013 }
01014
01015 const byte _road_sloped_sprites[14] = {
01016 0, 0, 2, 0,
01017 0, 1, 0, 0,
01018 3, 0, 0, 0,
01019 0, 0
01020 };
01021
01032 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
01033 {
01034 return (IsOnSnow(tile) &&
01035 !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
01036 roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
01037 }
01038
01044 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
01045 {
01046
01047 if (IsInvisibilitySet(TO_CATENARY)) return;
01048
01049
01050 if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
01051 uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
01052
01053 if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
01054 }
01055
01056 SpriteID front;
01057 SpriteID back;
01058
01059 if (ti->tileh != SLOPE_FLAT) {
01060 back = SPR_TRAMWAY_BACK_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
01061 front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
01062 } else {
01063 back = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
01064 front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
01065 }
01066
01067 AddSortableSpriteToDraw(back, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
01068 AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
01069 }
01070
01079 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
01080 {
01081 int x = ti->x | dx;
01082 int y = ti->y | dy;
01083 byte z = ti->z;
01084 if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
01085 AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
01086 }
01087
01092 static void DrawRoadBits(TileInfo *ti)
01093 {
01094 RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
01095 RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
01096
01097 SpriteID image = 0;
01098 PaletteID pal = PAL_NONE;
01099
01100 if (ti->tileh != SLOPE_FLAT) {
01101 DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
01102
01103
01104
01105 if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
01106 }
01107
01108 if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
01109
01110 Roadside roadside = GetRoadside(ti->tile);
01111
01112 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01113 image += 19;
01114 } else {
01115 switch (roadside) {
01116 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01117 case ROADSIDE_GRASS: break;
01118 case ROADSIDE_GRASS_ROAD_WORKS: break;
01119 default: image -= 19; break;
01120 }
01121 }
01122
01123 DrawGroundSprite(image, pal);
01124
01125
01126
01127
01128 if (tram != ROAD_NONE) {
01129 if (ti->tileh != SLOPE_FLAT) {
01130 image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
01131 } else {
01132 image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
01133 }
01134 image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
01135 DrawGroundSprite(image, pal);
01136 }
01137
01138 if (road != ROAD_NONE) {
01139 DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
01140 if (drd != DRD_NONE) {
01141 DrawGroundSpriteAt(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), PAL_NONE, 8, 8, GetPartialZ(8, 8, ti->tileh));
01142 }
01143 }
01144
01145 if (HasRoadWorks(ti->tile)) {
01146
01147 DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
01148 return;
01149 }
01150
01151 if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
01152
01153
01154 if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
01155
01156
01157 if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
01158 uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
01159 uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT;
01160
01161 if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT;
01162
01163 if (height < minz) return;
01164 }
01165
01166
01167 if (HasAtMostOneBit(road)) return;
01168
01169
01170 for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
01171 DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
01172 }
01173 }
01174
01176 static void DrawTile_Road(TileInfo *ti)
01177 {
01178 switch (GetRoadTileType(ti->tile)) {
01179 case ROAD_TILE_NORMAL:
01180 DrawRoadBits(ti);
01181 break;
01182
01183 case ROAD_TILE_CROSSING: {
01184 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
01185
01186 PaletteID pal = PAL_NONE;
01187 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
01188
01189 if (rti->UsesOverlay()) {
01190 Axis axis = GetCrossingRailAxis(ti->tile);
01191 SpriteID road = SPR_ROAD_Y + axis;
01192
01193 Roadside roadside = GetRoadside(ti->tile);
01194
01195 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01196 road += 19;
01197 } else {
01198 switch (roadside) {
01199 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01200 case ROADSIDE_GRASS: break;
01201 default: road -= 19; break;
01202 }
01203 }
01204
01205 DrawGroundSprite(road, pal);
01206
01207 SpriteID rail = GetCustomRailSprite(rti, ti->tile, RTSG_CROSSING) + axis;
01208 DrawGroundSprite(rail, PAL_NONE);
01209 DrawRailTileSeq(ti, &_crossing_layout, TO_CATENARY, rail, 0, PAL_NONE);
01210 } else {
01211 SpriteID image = rti->base_sprites.crossing;
01212
01213 if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
01214 if (IsCrossingBarred(ti->tile)) image += 2;
01215
01216 Roadside roadside = GetRoadside(ti->tile);
01217
01218 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01219 image += 8;
01220 } else {
01221 switch (roadside) {
01222 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01223 case ROADSIDE_GRASS: break;
01224 default: image += 4; break;
01225 }
01226 }
01227
01228 DrawGroundSprite(image, pal);
01229
01230
01231 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasCrossingReservation(ti->tile)) {
01232 DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y, PALETTE_CRASH);
01233 }
01234 }
01235
01236 if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
01237 DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
01238 DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile));
01239 }
01240 if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
01241 break;
01242 }
01243
01244 default:
01245 case ROAD_TILE_DEPOT: {
01246 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
01247
01248 PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
01249
01250 const DrawTileSprites *dts;
01251 if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
01252 dts = &_tram_depot[GetRoadDepotDirection(ti->tile)];
01253 } else {
01254 dts = &_road_depot[GetRoadDepotDirection(ti->tile)];
01255 }
01256
01257 DrawGroundSprite(dts->ground.sprite, PAL_NONE);
01258 DrawOrigTileSeq(ti, dts, TO_BUILDINGS, palette);
01259 break;
01260 }
01261 }
01262 DrawBridgeMiddle(ti);
01263 }
01264
01265 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
01266 {
01267 PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
01268 const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
01269
01270 x += 33;
01271 y += 17;
01272
01273 DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
01274 DrawOrigTileSeqInGUI(x, y, dts, palette);
01275 }
01276
01282 void UpdateNearestTownForRoadTiles(bool invalidate)
01283 {
01284 assert(!invalidate || _generating_world);
01285
01286 for (TileIndex t = 0; t < MapSize(); t++) {
01287 if (IsTileType(t, MP_ROAD) && !IsRoadDepot(t) && !HasTownOwnedRoad(t)) {
01288 TownID tid = (TownID)INVALID_TOWN;
01289 if (!invalidate) {
01290 const Town *town = CalcClosestTownFromTile(t);
01291 if (town != NULL) tid = town->index;
01292 }
01293 SetTownIndex(t, tid);
01294 }
01295 }
01296 }
01297
01298 static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
01299 {
01300 uint z;
01301 Slope tileh = GetTileSlope(tile, &z);
01302
01303 if (tileh == SLOPE_FLAT) return z;
01304 if (IsNormalRoad(tile)) {
01305 Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
01306 z += ApplyFoundationToSlope(f, &tileh);
01307 return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
01308 } else {
01309 return z + TILE_HEIGHT;
01310 }
01311 }
01312
01313 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
01314 {
01315 if (IsNormalRoad(tile)) {
01316 return GetRoadFoundation(tileh, GetAllRoadBits(tile));
01317 } else {
01318 return FlatteningFoundation(tileh);
01319 }
01320 }
01321
01322 static const Roadside _town_road_types[][2] = {
01323 { ROADSIDE_GRASS, ROADSIDE_GRASS },
01324 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01325 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01326 { ROADSIDE_TREES, ROADSIDE_TREES },
01327 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
01328 };
01329
01330 static const Roadside _town_road_types_2[][2] = {
01331 { ROADSIDE_GRASS, ROADSIDE_GRASS },
01332 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01333 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
01334 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
01335 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
01336 };
01337
01338
01339 static void TileLoop_Road(TileIndex tile)
01340 {
01341 switch (_settings_game.game_creation.landscape) {
01342 case LT_ARCTIC:
01343 if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
01344 ToggleSnow(tile);
01345 MarkTileDirtyByTile(tile);
01346 }
01347 break;
01348
01349 case LT_TROPIC:
01350 if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
01351 ToggleDesert(tile);
01352 MarkTileDirtyByTile(tile);
01353 }
01354 break;
01355 }
01356
01357 if (IsRoadDepot(tile)) return;
01358
01359 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
01360 if (!HasRoadWorks(tile)) {
01361 HouseZonesBits grp = HZB_TOWN_EDGE;
01362
01363 if (t != NULL) {
01364 grp = GetTownRadiusGroup(t, tile);
01365
01366
01367 if (t->road_build_months != 0 &&
01368 (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
01369 IsNormalRoad(tile) && !HasAtMostOneBit(GetAllRoadBits(tile))) {
01370 if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) {
01371 StartRoadWorks(tile);
01372
01373 SndPlayTileFx(SND_21_JACKHAMMER, tile);
01374 CreateEffectVehicleAbove(
01375 TileX(tile) * TILE_SIZE + 7,
01376 TileY(tile) * TILE_SIZE + 7,
01377 0,
01378 EV_BULLDOZER);
01379 MarkTileDirtyByTile(tile);
01380 return;
01381 }
01382 }
01383 }
01384
01385 {
01386
01387 const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
01388 Roadside cur_rs = GetRoadside(tile);
01389
01390
01391 if (cur_rs == new_rs[0]) return;
01392
01393
01394 if (cur_rs == new_rs[1]) {
01395 cur_rs = new_rs[0];
01396
01397 } else if (cur_rs == ROADSIDE_BARREN) {
01398 cur_rs = new_rs[1];
01399
01400 } else {
01401 cur_rs = ROADSIDE_BARREN;
01402 }
01403 SetRoadside(tile, cur_rs);
01404 MarkTileDirtyByTile(tile);
01405 }
01406 } else if (IncreaseRoadWorksCounter(tile)) {
01407 TerminateRoadWorks(tile);
01408
01409 if (_settings_game.economy.mod_road_rebuild) {
01410
01411 const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
01412 const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
01413
01414 if (old_rb != new_rb) {
01415 RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true);
01416 }
01417 }
01418
01419 MarkTileDirtyByTile(tile);
01420 }
01421 }
01422
01423 static bool ClickTile_Road(TileIndex tile)
01424 {
01425 if (!IsRoadDepot(tile)) return false;
01426
01427 ShowDepotWindow(tile, VEH_ROAD);
01428 return true;
01429 }
01430
01431
01432 static const byte _road_trackbits[16] = {
01433 0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F,
01434 };
01435
01436 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
01437 {
01438 TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
01439 TrackdirBits red_signals = TRACKDIR_BIT_NONE;
01440 switch (mode) {
01441 case TRANSPORT_RAIL:
01442 if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
01443 break;
01444
01445 case TRANSPORT_ROAD:
01446 if ((GetRoadTypes(tile) & sub_mode) == 0) break;
01447 switch (GetRoadTileType(tile)) {
01448 case ROAD_TILE_NORMAL: {
01449 const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
01450 RoadType rt = (RoadType)FindFirstBit(sub_mode);
01451 RoadBits bits = GetRoadBits(tile, rt);
01452
01453
01454 if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
01455
01456 uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
01457 if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
01458 break;
01459 }
01460
01461 case ROAD_TILE_CROSSING: {
01462 Axis axis = GetCrossingRoadAxis(tile);
01463
01464 if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
01465
01466 trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
01467 if (IsCrossingBarred(tile)) red_signals = trackdirbits;
01468 break;
01469 }
01470
01471 default:
01472 case ROAD_TILE_DEPOT: {
01473 DiagDirection dir = GetRoadDepotDirection(tile);
01474
01475 if (side != INVALID_DIAGDIR && side != dir) break;
01476
01477 trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
01478 break;
01479 }
01480 }
01481 break;
01482
01483 default: break;
01484 }
01485 return CombineTrackStatus(trackdirbits, red_signals);
01486 }
01487
01488 static const StringID _road_tile_strings[] = {
01489 STR_LAI_ROAD_DESCRIPTION_ROAD,
01490 STR_LAI_ROAD_DESCRIPTION_ROAD,
01491 STR_LAI_ROAD_DESCRIPTION_ROAD,
01492 STR_LAI_ROAD_DESCRIPTION_ROAD_WITH_STREETLIGHTS,
01493 STR_LAI_ROAD_DESCRIPTION_ROAD,
01494 STR_LAI_ROAD_DESCRIPTION_TREE_LINED_ROAD,
01495 STR_LAI_ROAD_DESCRIPTION_ROAD,
01496 STR_LAI_ROAD_DESCRIPTION_ROAD,
01497 };
01498
01499 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
01500 {
01501 Owner rail_owner = INVALID_OWNER;
01502 Owner road_owner = INVALID_OWNER;
01503 Owner tram_owner = INVALID_OWNER;
01504
01505 switch (GetRoadTileType(tile)) {
01506 case ROAD_TILE_CROSSING: {
01507 td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_RAIL_LEVEL_CROSSING;
01508 RoadTypes rts = GetRoadTypes(tile);
01509 rail_owner = GetTileOwner(tile);
01510 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01511 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01512
01513 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
01514 td->rail_speed = rti->max_speed;
01515
01516 break;
01517 }
01518
01519 case ROAD_TILE_DEPOT:
01520 td->str = STR_LAI_ROAD_DESCRIPTION_ROAD_VEHICLE_DEPOT;
01521 road_owner = GetTileOwner(tile);
01522 break;
01523
01524 default: {
01525 RoadTypes rts = GetRoadTypes(tile);
01526 td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_LAI_ROAD_DESCRIPTION_TRAMWAY);
01527 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01528 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01529 break;
01530 }
01531 }
01532
01533
01534
01535
01536
01537
01538 Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
01539 bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
01540
01541 if (mixed_owners) {
01542
01543 td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_RAIL_OWNER);
01544 td->owner[0] = rail_owner;
01545 td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_ROAD_OWNER);
01546 td->owner[1] = road_owner;
01547 td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_LAND_AREA_INFORMATION_TRAM_OWNER);
01548 td->owner[2] = tram_owner;
01549 } else {
01550
01551 td->owner[0] = first_owner;
01552 }
01553 }
01554
01559 static const byte _roadveh_enter_depot_dir[4] = {
01560 TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
01561 };
01562
01563 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
01564 {
01565 switch (GetRoadTileType(tile)) {
01566 case ROAD_TILE_DEPOT: {
01567 if (v->type != VEH_ROAD) break;
01568
01569 RoadVehicle *rv = RoadVehicle::From(v);
01570 if (rv->frame == RVC_DEPOT_STOP_FRAME &&
01571 _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == rv->state) {
01572 rv->state = RVSB_IN_DEPOT;
01573 rv->vehstatus |= VS_HIDDEN;
01574 rv->direction = ReverseDir(rv->direction);
01575 if (rv->Next() == NULL) VehicleEnterDepot(rv->First());
01576 rv->tile = tile;
01577
01578 InvalidateWindowData(WC_VEHICLE_DEPOT, rv->tile);
01579 return VETSB_ENTERED_WORMHOLE;
01580 }
01581 } break;
01582
01583 default: break;
01584 }
01585 return VETSB_CONTINUE;
01586 }
01587
01588
01589 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
01590 {
01591 if (IsRoadDepot(tile)) {
01592 if (GetTileOwner(tile) == old_owner) {
01593 if (new_owner == INVALID_OWNER) {
01594 DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
01595 } else {
01596 SetTileOwner(tile, new_owner);
01597 }
01598 }
01599 return;
01600 }
01601
01602 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
01603
01604 if (GetRoadOwner(tile, rt) == old_owner) {
01605 SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
01606 }
01607 }
01608
01609 if (IsLevelCrossing(tile)) {
01610 if (GetTileOwner(tile) == old_owner) {
01611 if (new_owner == INVALID_OWNER) {
01612 DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
01613 } else {
01614 SetTileOwner(tile, new_owner);
01615 }
01616 }
01617 }
01618 }
01619
01620 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
01621 {
01622 if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
01623 switch (GetRoadTileType(tile)) {
01624 case ROAD_TILE_CROSSING:
01625 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]);
01626 break;
01627
01628 case ROAD_TILE_DEPOT:
01629 if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01630 break;
01631
01632 case ROAD_TILE_NORMAL: {
01633 RoadBits bits = GetAllRoadBits(tile);
01634 RoadBits bits_copy = bits;
01635
01636 if (CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE).Succeeded()) {
01637
01638 if (bits == bits_copy) {
01639 uint z_old;
01640 Slope tileh_old = GetTileSlope(tile, &z_old);
01641
01642
01643 z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
01644 z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
01645
01646
01647 if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
01648 }
01649 }
01650 break;
01651 }
01652
01653 default: NOT_REACHED();
01654 }
01655 }
01656
01657 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
01658 }
01659
01661 extern const TileTypeProcs _tile_type_road_procs = {
01662 DrawTile_Road,
01663 GetSlopeZ_Road,
01664 ClearTile_Road,
01665 NULL,
01666 GetTileDesc_Road,
01667 GetTileTrackStatus_Road,
01668 ClickTile_Road,
01669 NULL,
01670 TileLoop_Road,
01671 ChangeTileOwner_Road,
01672 NULL,
01673 VehicleEnter_Road,
01674 GetFoundation_Road,
01675 TerraformTile_Road,
01676 };