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