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