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