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