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