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