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