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