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