00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "cmd_helper.h"
00008 #include "road_internal.h"
00009 #include "landscape.h"
00010 #include "town_map.h"
00011 #include "viewport_func.h"
00012 #include "command_func.h"
00013 #include "yapf/yapf.h"
00014 #include "depot_base.h"
00015 #include "newgrf.h"
00016 #include "station_map.h"
00017 #include "variables.h"
00018 #include "autoslope.h"
00019 #include "tunnelbridge_map.h"
00020 #include "window_func.h"
00021 #include "strings_func.h"
00022 #include "vehicle_func.h"
00023 #include "sound_func.h"
00024 #include "tunnelbridge.h"
00025 #include "cheat_type.h"
00026 #include "functions.h"
00027 #include "effectvehicle_func.h"
00028 #include "elrail_func.h"
00029 #include "roadveh.h"
00030
00031 #include "table/sprites.h"
00032 #include "table/strings.h"
00033
00038 bool RoadVehiclesAreBuilt()
00039 {
00040 const Vehicle *v;
00041
00042 FOR_ALL_VEHICLES(v) {
00043 if (v->type == VEH_ROAD) return true;
00044 }
00045 return false;
00046 }
00047
00048 #define M(x) (1 << (x))
00049
00050 static const uint32 VALID_LEVEL_CROSSING_SLOPES = (M(SLOPE_SEN) | M(SLOPE_ENW) | M(SLOPE_NWS) | M(SLOPE_NS) | M(SLOPE_WSE) | M(SLOPE_EW) | M(SLOPE_FLAT));
00051 #undef M
00052
00053
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 Foundation GetRoadFoundation(Slope tileh, RoadBits bits);
00103
00114 bool 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 true;
00117
00118
00119
00120
00121 if (_current_company == OWNER_WATER ||
00122 (rt == ROADTYPE_ROAD && !IsValidCompanyID(_current_company))) return true;
00123
00124
00125
00126 if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner);
00127
00128 if (!town_check) return true;
00129
00130 if (_cheats.magic_bulldozer.value) return true;
00131
00132 Town *t = ClosestTownFromTile(tile, UINT_MAX);
00133 if (t == NULL) return true;
00134
00135
00136
00137 if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return false;
00138
00139
00140 RoadBits n = ROAD_NONE;
00141 RoadBits present = GetAnyRoadBits(tile, rt);
00142 if (present & ROAD_NE && GetAnyRoadBits(TILE_ADDXY(tile, -1, 0), rt) & ROAD_SW) n |= ROAD_NE;
00143 if (present & ROAD_SE && GetAnyRoadBits(TILE_ADDXY(tile, 0, 1), rt) & ROAD_NW) n |= ROAD_SE;
00144 if (present & ROAD_SW && GetAnyRoadBits(TILE_ADDXY(tile, 1, 0), rt) & ROAD_NE) n |= ROAD_SW;
00145 if (present & ROAD_NW && GetAnyRoadBits(TILE_ADDXY(tile, 0, -1), rt) & ROAD_SE) n |= ROAD_NW;
00146
00147 int rating_decrease = RATING_ROAD_DOWN_STEP_EDGE;
00148
00149
00150 if (KillFirstBit(n) != ROAD_NONE && (n & remove) != ROAD_NONE) {
00151
00152 if (!_settings_game.construction.extra_dynamite) {
00153 SetDParam(0, t->index);
00154 _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
00155 return false;
00156 }
00157 rating_decrease = RATING_ROAD_DOWN_STEP_INNER;
00158 }
00159 ChangeTownRating(t, rating_decrease, RATING_ROAD_MINIMUM, flags);
00160
00161 return true;
00162 }
00163
00164
00172 static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits pieces, RoadType rt, bool crossing_check, bool town_check = true)
00173 {
00174 RoadTypes rts = GetRoadTypes(tile);
00175
00176 if (!HasBit(rts, rt)) return CMD_ERROR;
00177
00178 switch (GetTileType(tile)) {
00179 case MP_ROAD:
00180 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00181 break;
00182
00183 case MP_STATION:
00184 if (!IsDriveThroughStopTile(tile)) return CMD_ERROR;
00185 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00186 break;
00187
00188 case MP_TUNNELBRIDGE:
00189 if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR;
00190 if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
00191 break;
00192
00193 default:
00194 return CMD_ERROR;
00195 }
00196
00197 if (!CheckAllowRemoveRoad(tile, pieces, GetRoadOwner(tile, rt), rt, flags, town_check)) return CMD_ERROR;
00198
00199 if (!IsTileType(tile, MP_ROAD)) {
00200
00201 if (rts == RoadTypeToRoadTypes(rt)) return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00202
00203 CommandCost cost(EXPENSES_CONSTRUCTION);
00204 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00205 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
00206
00207 cost.AddCost((GetTunnelBridgeLength(other_end, tile) + 2) * _price.remove_road);
00208 if (flags & DC_EXEC) {
00209 SetRoadTypes(other_end, GetRoadTypes(other_end) & ~RoadTypeToRoadTypes(rt));
00210 SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
00211
00212
00213
00214 RoadType other_rt = (rt == ROADTYPE_ROAD) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
00215 Owner other_owner = GetRoadOwner(tile, other_rt);
00216 if (other_owner != GetTileOwner(tile)) {
00217 SetTileOwner(tile, other_owner);
00218 SetTileOwner(other_end, other_owner);
00219 }
00220
00221
00222 MarkTileDirtyByTile(tile);
00223 MarkTileDirtyByTile(other_end);
00224 if (IsBridge(tile)) {
00225 TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
00226
00227 for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
00228 }
00229 }
00230 } else {
00231 assert(IsDriveThroughStopTile(tile));
00232 cost.AddCost(_price.remove_road * 2);
00233 if (flags & DC_EXEC) {
00234 SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt));
00235 MarkTileDirtyByTile(tile);
00236 }
00237 }
00238 return cost;
00239 }
00240
00241 switch (GetRoadTileType(tile)) {
00242 case ROAD_TILE_NORMAL: {
00243 const Slope tileh = GetTileSlope(tile, NULL);
00244 RoadBits present = GetRoadBits(tile, rt);
00245 const RoadBits other = GetOtherRoadBits(tile, rt);
00246 const Foundation f = GetRoadFoundation(tileh, present);
00247
00248 if (HasRoadWorks(tile) && _current_company != OWNER_WATER) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
00249
00250
00251
00252
00253
00254 if (IsSteepSlope(tileh) || (IsStraightRoad(other) &&
00255 (other & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) != ROAD_NONE) ||
00256 (tileh != SLOPE_FLAT && !_settings_game.construction.build_on_slopes)) {
00257 pieces |= MirrorRoadBits(pieces);
00258 }
00259
00260
00261 pieces &= present;
00262 if (pieces == ROAD_NONE) return CMD_ERROR;
00263
00264
00265 present ^= pieces;
00266
00267
00268 if (tileh != SLOPE_FLAT && present != ROAD_NONE &&
00269 (present & _invalid_tileh_slopes_road[0][tileh & SLOPE_ELEVATED]) == present) {
00270 return CMD_ERROR;
00271 }
00272
00273 if (flags & DC_EXEC) {
00274 if (HasRoadWorks(tile)) {
00275
00276 assert(_current_company == OWNER_WATER);
00277 Vehicle *v;
00278 FOR_ALL_VEHICLES(v) {
00279 if (v->type == VEH_EFFECT && TileVirtXY(v->x_pos, v->y_pos) == tile) {
00280 delete v;
00281 }
00282 }
00283 }
00284 if (present == ROAD_NONE) {
00285 RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
00286 if (rts == ROADTYPES_NONE) {
00287
00288 DoClearSquare(tile);
00289 } else {
00290 if (rt == ROADTYPE_ROAD && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) {
00291
00292 const Town *town = CalcClosestTownFromTile(tile);
00293 SetTownIndex(tile, town == NULL ? (TownID)INVALID_TOWN : town->index);
00294 }
00295 SetRoadBits(tile, ROAD_NONE, rt);
00296 SetRoadTypes(tile, rts);
00297 MarkTileDirtyByTile(tile);
00298 }
00299 } else {
00300
00301
00302
00303 if (rt != ROADTYPE_TRAM) SetDisallowedRoadDirections(tile, DRD_NONE);
00304 SetRoadBits(tile, present, rt);
00305 MarkTileDirtyByTile(tile);
00306 }
00307 }
00308
00309
00310 return CommandCost(EXPENSES_CONSTRUCTION, CountBits(pieces) * _price.remove_road +
00311 ((GetRoadFoundation(tileh, present) != f) ? _price.terraform : (Money)0));
00312 }
00313
00314 case ROAD_TILE_CROSSING: {
00315 if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) {
00316 return CMD_ERROR;
00317 }
00318
00319
00320
00321 if (rt == ROADTYPE_ROAD && HasTileRoadType(tile, ROADTYPE_TRAM) && (flags & DC_EXEC || crossing_check)) return CMD_ERROR;
00322
00323 if (flags & DC_EXEC) {
00324 Track railtrack = GetCrossingRailTrack(tile);
00325 RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
00326 if (rts == ROADTYPES_NONE) {
00327 TrackBits tracks = GetCrossingRailBits(tile);
00328 bool reserved = GetCrossingReservation(tile);
00329 MakeRailNormal(tile, GetTileOwner(tile), tracks, GetRailType(tile));
00330 if (reserved) SetTrackReservation(tile, tracks);
00331 } else {
00332 SetRoadTypes(tile, rts);
00333
00334 }
00335 MarkTileDirtyByTile(tile);
00336 YapfNotifyTrackLayoutChange(tile, railtrack);
00337 }
00338 return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_road * 2);
00339 }
00340
00341 default:
00342 case ROAD_TILE_DEPOT:
00343 return CMD_ERROR;
00344 }
00345 }
00346
00347
00355 CommandCost CmdRemoveRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00356 {
00357 RoadType rt = (RoadType)GB(p1, 4, 2);
00358 if (!IsValidRoadType(rt)) return CMD_ERROR;
00359
00360 RoadBits pieces = Extract<RoadBits, 0>(p1);
00361
00362 return RemoveRoad(tile, flags, pieces, rt, true);
00363 }
00364
00376 static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
00377 {
00378
00379 CLRBITS(*pieces, existing);
00380
00381
00382 if (*pieces == ROAD_NONE) return CMD_ERROR;
00383
00384
00385 if (tileh == SLOPE_FLAT) return CommandCost();
00386
00387
00388 if (IsSteepSlope(tileh)) {
00389
00390 *pieces |= MirrorRoadBits(*pieces);
00391
00392
00393
00394 existing |= other;
00395
00396 if ((existing == ROAD_NONE || existing == *pieces) && IsStraightRoad(*pieces)) {
00397 return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
00398 }
00399 return CMD_ERROR;
00400 }
00401
00402
00403 RoadBits type_bits = existing | *pieces;
00404
00405
00406 if (_settings_game.construction.build_on_slopes && (_invalid_tileh_slopes_road[0][tileh] & (other | type_bits)) == ROAD_NONE) {
00407
00408
00409 if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
00410
00411 return CommandCost();
00412 }
00413
00414
00415 *pieces |= MirrorRoadBits(*pieces);
00416 type_bits = existing | *pieces;
00417
00418
00419 if (IsStraightRoad(type_bits) && (other == type_bits || other == ROAD_NONE) &&
00420 (_invalid_tileh_slopes_road[1][tileh] & (other | type_bits)) == ROAD_NONE) {
00421
00422
00423 if (IsSlopeWithOneCornerRaised(tileh)) {
00424
00425
00426 if (_settings_game.construction.build_on_slopes) {
00427
00428
00429 if ((other | existing) == ROAD_NONE) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
00430
00431 return CommandCost();
00432 }
00433 } else {
00434 if (CountBits(existing) == 1) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
00435 return CommandCost();
00436 }
00437 }
00438 return CMD_ERROR;
00439 }
00440
00449 CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00450 {
00451 CommandCost cost(EXPENSES_CONSTRUCTION);
00452
00453 RoadBits existing = ROAD_NONE;
00454 RoadBits other_bits = ROAD_NONE;
00455
00456
00457
00458 if ((IsValidCompanyID(_current_company) && p2 != 0) || (_current_company == OWNER_TOWN && !IsValidTownID(p2))) return CMD_ERROR;
00459 if (_current_company != OWNER_TOWN) {
00460 const Town *town = CalcClosestTownFromTile(tile);
00461 p2 = (town != NULL) ? town->index : (TownID)INVALID_TOWN;
00462 }
00463
00464 RoadBits pieces = Extract<RoadBits, 0>(p1);
00465
00466
00467 if (pieces == ROAD_NONE) return CMD_ERROR;
00468
00469 RoadType rt = (RoadType)GB(p1, 4, 2);
00470 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00471
00472 DisallowedRoadDirections toggle_drd = (DisallowedRoadDirections)GB(p1, 6, 2);
00473
00474 Slope tileh = GetTileSlope(tile, NULL);
00475
00476 switch (GetTileType(tile)) {
00477 case MP_ROAD:
00478 switch (GetRoadTileType(tile)) {
00479 case ROAD_TILE_NORMAL: {
00480 if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
00481
00482 other_bits = GetOtherRoadBits(tile, rt);
00483 if (!HasTileRoadType(tile, rt)) break;
00484
00485 existing = GetRoadBits(tile, rt);
00486 bool crossing = !IsStraightRoad(existing | pieces);
00487 if (rt != ROADTYPE_TRAM && (GetDisallowedRoadDirections(tile) != DRD_NONE || toggle_drd != DRD_NONE) && crossing) {
00488
00489 return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
00490 }
00491 if ((existing & pieces) == pieces) {
00492
00493 if (toggle_drd != DRD_NONE && rt != ROADTYPE_TRAM && IsRoadOwner(tile, ROADTYPE_ROAD, _current_company)) {
00494 if (crossing) return_cmd_error(STR_ERR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION);
00495
00496 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00497
00498
00499 if (flags & DC_EXEC && rt != ROADTYPE_TRAM && IsStraightRoad(existing)) {
00500 SetDisallowedRoadDirections(tile, GetDisallowedRoadDirections(tile) ^ toggle_drd);
00501 MarkTileDirtyByTile(tile);
00502 }
00503 return CommandCost();
00504 }
00505 return_cmd_error(STR_1007_ALREADY_BUILT);
00506 }
00507 } break;
00508
00509 case ROAD_TILE_CROSSING:
00510 other_bits = GetCrossingRoadBits(tile);
00511 if (pieces & ComplementRoadBits(other_bits)) goto do_clear;
00512 pieces = other_bits;
00513
00514 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
00515 break;
00516
00517 default:
00518 case ROAD_TILE_DEPOT:
00519 goto do_clear;
00520 }
00521 break;
00522
00523 case MP_RAILWAY: {
00524 if (IsSteepSlope(tileh)) {
00525 return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
00526 }
00527
00528
00529 if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) {
00530 return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
00531 }
00532
00533 if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
00534
00535 Axis roaddir;
00536 switch (GetTrackBits(tile)) {
00537 case TRACK_BIT_X:
00538 if (pieces & ROAD_X) goto do_clear;
00539 roaddir = AXIS_Y;
00540 break;
00541
00542 case TRACK_BIT_Y:
00543 if (pieces & ROAD_Y) goto do_clear;
00544 roaddir = AXIS_X;
00545 break;
00546
00547 default: goto do_clear;
00548 }
00549
00550 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00551
00552 if (flags & DC_EXEC) {
00553 Track railtrack = AxisToTrack(OtherAxis(roaddir));
00554 YapfNotifyTrackLayoutChange(tile, railtrack);
00555
00556 bool reserved = HasBit(GetTrackReservation(tile), railtrack);
00557 MakeRoadCrossing(tile, _current_company, _current_company, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt) | ROADTYPES_ROAD, p2);
00558 SetCrossingReservation(tile, reserved);
00559 UpdateLevelCrossing(tile, false);
00560 MarkTileDirtyByTile(tile);
00561 }
00562 return CommandCost(EXPENSES_CONSTRUCTION, _price.build_road * (rt == ROADTYPE_ROAD ? 2 : 4));
00563 }
00564
00565 case MP_STATION: {
00566 if (!IsDriveThroughStopTile(tile)) goto do_clear;
00567
00568 RoadBits curbits = AxisToRoadBits(DiagDirToAxis(GetRoadStopDir(tile)));
00569 if (pieces & ~curbits) goto do_clear;
00570 pieces = curbits;
00571
00572 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
00573 } break;
00574
00575 case MP_TUNNELBRIDGE:
00576 if (GetTunnelBridgeTransportType(tile) != TRANSPORT_ROAD) return CMD_ERROR;
00577 if (MirrorRoadBits(DiagDirToRoadBits(GetTunnelBridgeDirection(tile))) != pieces) return CMD_ERROR;
00578 if (HasTileRoadType(tile, rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
00579
00580 if (HasVehicleOnTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile))) return CMD_ERROR;
00581 break;
00582
00583 default: {
00584 do_clear:;
00585 CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00586 if (CmdFailed(ret)) return ret;
00587 cost.AddCost(ret);
00588 } break;
00589 }
00590
00591 if (other_bits != pieces) {
00592
00593 CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
00594
00595
00596 if (CmdFailed(ret) || (ret.GetCost() != 0 && !_settings_game.construction.build_on_slopes)) {
00597 return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
00598 }
00599 cost.AddCost(ret);
00600 }
00601
00602 if (IsTileType(tile, MP_ROAD)) {
00603
00604 pieces &= ComplementRoadBits(existing);
00605
00606
00607 if (IsNormalRoad(tile)) {
00608 Slope slope = GetTileSlope(tile, NULL);
00609 Foundation found_new = GetRoadFoundation(slope, pieces | existing);
00610
00611
00612 for (RoadType rtest = ROADTYPE_ROAD; rtest < ROADTYPE_END; rtest++) {
00613 if (rtest != rt) {
00614 RoadBits bits = GetRoadBits(tile, rtest);
00615
00616 if (bits != ROAD_NONE && GetRoadFoundation(slope, bits) != found_new) {
00617 return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
00618 }
00619 }
00620 }
00621 }
00622 }
00623
00624 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00625
00626 cost.AddCost(CountBits(pieces) * _price.build_road);
00627 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00628
00629 cost.MultiplyCost(GetTunnelBridgeLength(GetOtherTunnelBridgeEnd(tile), tile) + 2);
00630 }
00631
00632 if (flags & DC_EXEC) {
00633 switch (GetTileType(tile)) {
00634 case MP_ROAD: {
00635 RoadTileType rtt = GetRoadTileType(tile);
00636 if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
00637 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00638 SetRoadOwner(tile, rt, _current_company);
00639 if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
00640 }
00641 if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
00642 } break;
00643
00644 case MP_TUNNELBRIDGE: {
00645 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
00646
00647 SetRoadTypes(other_end, GetRoadTypes(other_end) | RoadTypeToRoadTypes(rt));
00648 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00649 SetRoadOwner(other_end, rt, _current_company);
00650 SetRoadOwner(tile, rt, _current_company);
00651
00652
00653 MarkTileDirtyByTile(other_end);
00654 MarkTileDirtyByTile(tile);
00655 if (IsBridge(tile)) {
00656 TileIndexDiff delta = TileOffsByDiagDir(GetTunnelBridgeDirection(tile));
00657
00658 for (TileIndex t = tile + delta; t != other_end; t += delta) MarkTileDirtyByTile(t);
00659 }
00660 } break;
00661
00662 case MP_STATION:
00663 assert(IsDriveThroughStopTile(tile));
00664 SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
00665 SetRoadOwner(tile, rt, _current_company);
00666 break;
00667
00668 default:
00669 MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_company, _current_company);
00670 break;
00671 }
00672
00673 if (rt != ROADTYPE_TRAM && IsNormalRoadTile(tile)) {
00674 existing |= pieces;
00675 SetDisallowedRoadDirections(tile, IsStraightRoad(existing) ?
00676 GetDisallowedRoadDirections(tile) ^ toggle_drd : DRD_NONE);
00677 }
00678
00679 MarkTileDirtyByTile(tile);
00680 }
00681 return cost;
00682 }
00683
00695 CommandCost CmdBuildLongRoad(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00696 {
00697 CommandCost cost(EXPENSES_CONSTRUCTION);
00698 bool had_bridge = false;
00699 bool had_tunnel = false;
00700 bool had_success = false;
00701 DisallowedRoadDirections drd = DRD_NORTHBOUND;
00702
00703 _error_message = INVALID_STRING_ID;
00704
00705 if (p1 >= MapSize()) return CMD_ERROR;
00706
00707 TileIndex start_tile = p1;
00708 RoadType rt = (RoadType)GB(p2, 3, 2);
00709 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00710
00711
00712 if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR;
00713 if (HasBit(p2, 2) && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR;
00714
00715
00716 if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
00717 TileIndex t = start_tile;
00718 start_tile = end_tile;
00719 end_tile = t;
00720 p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
00721 drd = DRD_SOUTHBOUND;
00722 }
00723
00724
00725
00726
00727 if (HasBit(p2, 2) == (start_tile == end_tile && HasBit(p2, 0) == HasBit(p2, 1))) drd ^= DRD_BOTH;
00728
00729 if (!HasBit(p2, 5)) drd = DRD_NONE;
00730
00731 TileIndex tile = start_tile;
00732
00733 for (;;) {
00734 RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X;
00735
00736 if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
00737 if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
00738
00739 _error_message = INVALID_STRING_ID;
00740 CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
00741 if (CmdFailed(ret)) {
00742 if (_error_message != STR_1007_ALREADY_BUILT) return CMD_ERROR;
00743 } else {
00744 had_success = true;
00745
00746 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00747 if (IsBridge(tile)) {
00748 if ((!had_bridge || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) {
00749 cost.AddCost(ret);
00750 }
00751 had_bridge = true;
00752 } else {
00753 if ((!had_tunnel || GetTunnelBridgeDirection(tile) == DIAGDIR_SE || GetTunnelBridgeDirection(tile) == DIAGDIR_SW)) {
00754 cost.AddCost(ret);
00755 }
00756 had_tunnel = true;
00757 }
00758 } else {
00759 cost.AddCost(ret);
00760 }
00761 }
00762
00763 if (tile == end_tile) break;
00764
00765 tile += HasBit(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
00766 }
00767
00768 return !had_success ? CMD_ERROR : cost;
00769 }
00770
00781 CommandCost CmdRemoveLongRoad(TileIndex end_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00782 {
00783 CommandCost cost(EXPENSES_CONSTRUCTION);
00784
00785 if (p1 >= MapSize()) return CMD_ERROR;
00786
00787 TileIndex start_tile = p1;
00788 RoadType rt = (RoadType)GB(p2, 3, 2);
00789 if (!IsValidRoadType(rt)) return CMD_ERROR;
00790
00791
00792 if (!HasBit(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR;
00793 if (HasBit(p2, 2) && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR;
00794
00795
00796 if (start_tile > end_tile || (start_tile == end_tile && HasBit(p2, 0))) {
00797 TileIndex t = start_tile;
00798 start_tile = end_tile;
00799 end_tile = t;
00800 p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
00801 }
00802
00803 Money money = GetAvailableMoneyForCommand();
00804 TileIndex tile = start_tile;
00805
00806 for (;;) {
00807 RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X;
00808
00809 if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
00810 if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
00811
00812
00813 if (bits != 0) {
00814 CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
00815 if (CmdSucceeded(ret)) {
00816 if (flags & DC_EXEC) {
00817 money -= ret.GetCost();
00818 if (money < 0) {
00819 _additional_cash_required = DoCommand(end_tile, start_tile, p2, flags & ~DC_EXEC, CMD_REMOVE_LONG_ROAD).GetCost();
00820 return cost;
00821 }
00822 RemoveRoad(tile, flags, bits, rt, true, false);
00823 }
00824 cost.AddCost(ret);
00825 }
00826 }
00827
00828 if (tile == end_tile) break;
00829
00830 tile += HasBit(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
00831 }
00832
00833 return (cost.GetCost() == 0) ? CMD_ERROR : cost;
00834 }
00835
00846 CommandCost CmdBuildRoadDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00847 {
00848 DiagDirection dir = Extract<DiagDirection, 0>(p1);
00849 RoadType rt = (RoadType)GB(p1, 2, 2);
00850
00851 if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
00852
00853 Slope tileh = GetTileSlope(tile, NULL);
00854 if (tileh != SLOPE_FLAT && (
00855 !_settings_game.construction.build_on_slopes ||
00856 IsSteepSlope(tileh) ||
00857 !CanBuildDepotByTileh(dir, tileh)
00858 )) {
00859 return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
00860 }
00861
00862 CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00863 if (CmdFailed(cost)) return CMD_ERROR;
00864
00865 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00866
00867 if (!Depot::CanAllocateItem()) return CMD_ERROR;
00868
00869 if (flags & DC_EXEC) {
00870 Depot *dep = new Depot(tile);
00871 dep->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
00872
00873 MakeRoadDepot(tile, _current_company, dir, rt, dep->town_index);
00874 MarkTileDirtyByTile(tile);
00875 }
00876 return cost.AddCost(_price.build_road_depot);
00877 }
00878
00879 static CommandCost RemoveRoadDepot(TileIndex tile, DoCommandFlag flags)
00880 {
00881 if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
00882
00883 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00884
00885 if (flags & DC_EXEC) {
00886 DoClearSquare(tile);
00887 delete GetDepotByTile(tile);
00888 }
00889
00890 return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_road_depot);
00891 }
00892
00893 static CommandCost ClearTile_Road(TileIndex tile, DoCommandFlag flags)
00894 {
00895 switch (GetRoadTileType(tile)) {
00896 case ROAD_TILE_NORMAL: {
00897 RoadBits b = GetAllRoadBits(tile);
00898
00899
00900 if ((CountBits(b) == 1 && GetRoadBits(tile, ROADTYPE_TRAM) == ROAD_NONE) || !(flags & DC_AUTO)) {
00901 RoadTypes rts = GetRoadTypes(tile);
00902 CommandCost ret(EXPENSES_CONSTRUCTION);
00903 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
00904 if (HasBit(rts, rt)) {
00905 CommandCost tmp_ret = RemoveRoad(tile, flags, GetRoadBits(tile, rt), rt, true);
00906 if (CmdFailed(tmp_ret)) return tmp_ret;
00907 ret.AddCost(tmp_ret);
00908 }
00909 }
00910 return ret;
00911 }
00912 return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
00913 }
00914
00915 case ROAD_TILE_CROSSING: {
00916 RoadTypes rts = GetRoadTypes(tile);
00917 CommandCost ret(EXPENSES_CONSTRUCTION);
00918
00919 if (flags & DC_AUTO) return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
00920
00921
00922
00923 RoadType rt = ROADTYPE_TRAM;
00924 do {
00925 if (HasBit(rts, rt)) {
00926 CommandCost tmp_ret = RemoveRoad(tile, flags, GetCrossingRoadBits(tile), rt, false);
00927 if (CmdFailed(tmp_ret)) return tmp_ret;
00928 ret.AddCost(tmp_ret);
00929 }
00930 } while (rt-- != ROADTYPE_ROAD);
00931
00932 if (flags & DC_EXEC) {
00933 DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00934 }
00935 return ret;
00936 }
00937
00938 default:
00939 case ROAD_TILE_DEPOT:
00940 if (flags & DC_AUTO) {
00941 return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
00942 }
00943 return RemoveRoadDepot(tile, flags);
00944 }
00945 }
00946
00947
00948 struct DrawRoadTileStruct {
00949 uint16 image;
00950 byte subcoord_x;
00951 byte subcoord_y;
00952 };
00953
00954 #include "table/road_land.h"
00955
00963 Foundation GetRoadFoundation(Slope tileh, RoadBits bits)
00964 {
00965
00966 if (tileh == SLOPE_FLAT || bits == ROAD_NONE) return FOUNDATION_NONE;
00967
00968 if (!IsSteepSlope(tileh)) {
00969
00970 if ((_invalid_tileh_slopes_road[0][tileh] & bits) == ROAD_NONE) return FOUNDATION_LEVELED;
00971
00972
00973 if (!IsSlopeWithOneCornerRaised(tileh) &&
00974 (_invalid_tileh_slopes_road[1][tileh] & bits) == ROAD_NONE)
00975 return FOUNDATION_NONE;
00976 }
00977
00978
00979 return (bits == ROAD_X ? FOUNDATION_INCLINED_X : FOUNDATION_INCLINED_Y);
00980 }
00981
00982 const byte _road_sloped_sprites[14] = {
00983 0, 0, 2, 0,
00984 0, 1, 0, 0,
00985 3, 0, 0, 0,
00986 0, 0
00987 };
00988
00999 static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
01000 {
01001 return (IsOnSnow(tile) &&
01002 !(_settings_game.game_creation.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
01003 roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
01004 }
01005
01011 void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
01012 {
01013
01014 if (IsInvisibilitySet(TO_CATENARY)) return;
01015
01016
01017 if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && !IsTransparencySet(TO_CATENARY)) {
01018 uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
01019
01020 if (height <= GetTileMaxZ(ti->tile) + TILE_HEIGHT) return;
01021 }
01022
01023 SpriteID front;
01024 SpriteID back;
01025
01026 if (ti->tileh != SLOPE_FLAT) {
01027 back = SPR_TRAMWAY_BACK_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
01028 front = SPR_TRAMWAY_FRONT_WIRES_SLOPED + _road_sloped_sprites[ti->tileh - 1];
01029 } else {
01030 back = SPR_TRAMWAY_BASE + _road_backpole_sprites_1[tram];
01031 front = SPR_TRAMWAY_BASE + _road_frontwire_sprites_1[tram];
01032 }
01033
01034 AddSortableSpriteToDraw(back, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
01035 AddSortableSpriteToDraw(front, PAL_NONE, ti->x, ti->y, 16, 16, TILE_HEIGHT + BB_HEIGHT_UNDER_BRIDGE, ti->z, IsTransparencySet(TO_CATENARY));
01036 }
01037
01046 static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
01047 {
01048 int x = ti->x | dx;
01049 int y = ti->y | dy;
01050 byte z = ti->z;
01051 if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
01052 AddSortableSpriteToDraw(img, PAL_NONE, x, y, 2, 2, h, z);
01053 }
01054
01059 static void DrawRoadBits(TileInfo *ti)
01060 {
01061 RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
01062 RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
01063
01064 SpriteID image = 0;
01065 SpriteID pal = PAL_NONE;
01066
01067 if (ti->tileh != SLOPE_FLAT) {
01068 DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
01069
01070
01071
01072 if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
01073 }
01074
01075 if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
01076
01077 Roadside roadside = GetRoadside(ti->tile);
01078
01079 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01080 image += 19;
01081 } else {
01082 switch (roadside) {
01083 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01084 case ROADSIDE_GRASS: break;
01085 case ROADSIDE_GRASS_ROAD_WORKS: break;
01086 default: image -= 19; break;
01087 }
01088 }
01089
01090 DrawGroundSprite(image, pal);
01091
01092
01093
01094
01095 if (tram != ROAD_NONE) {
01096 if (ti->tileh != SLOPE_FLAT) {
01097 image = _road_sloped_sprites[ti->tileh - 1] + SPR_TRAMWAY_SLOPED_OFFSET;
01098 } else {
01099 image = _road_tile_sprites_1[tram] - SPR_ROAD_Y;
01100 }
01101 image += (road == ROAD_NONE) ? SPR_TRAMWAY_TRAM : SPR_TRAMWAY_OVERLAY;
01102 DrawGroundSprite(image, pal);
01103 }
01104
01105 if (road != ROAD_NONE) {
01106 DisallowedRoadDirections drd = GetDisallowedRoadDirections(ti->tile);
01107 if (drd != DRD_NONE) {
01108 DrawRoadDetail(SPR_ONEWAY_BASE + drd - 1 + ((road == ROAD_X) ? 0 : 3), ti, 8, 8, 0);
01109 }
01110 }
01111
01112 if (HasRoadWorks(ti->tile)) {
01113
01114 DrawGroundSprite((road | tram) & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
01115 return;
01116 }
01117
01118 if (tram != ROAD_NONE) DrawTramCatenary(ti, tram);
01119
01120
01121 if (!HasBit(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
01122
01123
01124 if (MayHaveBridgeAbove(ti->tile) && IsBridgeAbove(ti->tile) && (roadside == ROADSIDE_TREES || roadside == ROADSIDE_STREET_LIGHTS)) {
01125 uint height = GetBridgeHeight(GetNorthernBridgeEnd(ti->tile));
01126 uint minz = GetTileMaxZ(ti->tile) + 2 * TILE_HEIGHT;
01127
01128 if (roadside == ROADSIDE_TREES) minz += TILE_HEIGHT;
01129
01130 if (height < minz) return;
01131 }
01132
01133
01134 if (CountBits(road) < 2) return;
01135
01136
01137 for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
01138 DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
01139 }
01140 }
01141
01143 static void DrawTile_Road(TileInfo *ti)
01144 {
01145 switch (GetRoadTileType(ti->tile)) {
01146 case ROAD_TILE_NORMAL:
01147 DrawRoadBits(ti);
01148 break;
01149
01150 case ROAD_TILE_CROSSING: {
01151 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
01152
01153 SpriteID image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.crossing;
01154 SpriteID pal = PAL_NONE;
01155
01156 if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
01157 if (IsCrossingBarred(ti->tile)) image += 2;
01158
01159 Roadside roadside = GetRoadside(ti->tile);
01160
01161 if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
01162 image += 8;
01163 } else {
01164 switch (roadside) {
01165 case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
01166 case ROADSIDE_GRASS: break;
01167 default: image += 4; break;
01168 }
01169 }
01170
01171 DrawGroundSprite(image, pal);
01172
01173
01174 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && GetCrossingReservation(ti->tile)) {
01175 DrawGroundSprite(GetCrossingRoadAxis(ti->tile) == AXIS_Y ? GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_y : GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.single_x, PALETTE_CRASH);
01176 }
01177
01178 if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
01179 DrawGroundSprite(SPR_TRAMWAY_OVERLAY + (GetCrossingRoadAxis(ti->tile) ^ 1), pal);
01180 DrawTramCatenary(ti, GetCrossingRoadBits(ti->tile));
01181 }
01182 if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
01183 break;
01184 }
01185
01186 default:
01187 case ROAD_TILE_DEPOT: {
01188 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
01189
01190 SpriteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
01191
01192 const DrawTileSprites *dts;
01193 if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
01194 dts = &_tram_depot[GetRoadDepotDirection(ti->tile)];
01195 } else {
01196 dts = &_road_depot[GetRoadDepotDirection(ti->tile)];
01197 }
01198
01199 DrawGroundSprite(dts->ground.sprite, PAL_NONE);
01200
01201
01202 if (IsInvisibilitySet(TO_BUILDINGS)) break;
01203
01204 for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) {
01205 SpriteID image = dtss->image.sprite;
01206 SpriteID pal;
01207
01208 if (!IsTransparencySet(TO_BUILDINGS) && HasBit(image, PALETTE_MODIFIER_COLOUR)) {
01209 pal = palette;
01210 } else {
01211 pal = PAL_NONE;
01212 }
01213
01214 AddSortableSpriteToDraw(
01215 image, pal,
01216 ti->x + dtss->delta_x, ti->y + dtss->delta_y,
01217 dtss->size_x, dtss->size_y,
01218 dtss->size_z, ti->z,
01219 IsTransparencySet(TO_BUILDINGS)
01220 );
01221 }
01222 break;
01223 }
01224 }
01225 DrawBridgeMiddle(ti);
01226 }
01227
01228 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
01229 {
01230 SpriteID palette = COMPANY_SPRITE_COLOUR(_local_company);
01231 const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
01232
01233 x += 33;
01234 y += 17;
01235
01236 DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
01237
01238 for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) {
01239 Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
01240 SpriteID image = dtss->image.sprite;
01241
01242 DrawSprite(image, HasBit(image, PALETTE_MODIFIER_COLOUR) ? palette : PAL_NONE, x + pt.x, y + pt.y);
01243 }
01244 }
01245
01250 void UpdateNearestTownForRoadTiles(bool invalidate)
01251 {
01252 assert(!invalidate || _generating_world);
01253
01254 for (TileIndex t = 0; t < MapSize(); t++) {
01255 if (IsTileType(t, MP_ROAD) && !HasTownOwnedRoad(t)) {
01256 TownID tid = (TownID)INVALID_TOWN;
01257 if (!invalidate) {
01258 const Town *town = CalcClosestTownFromTile(t);
01259 if (town != NULL) tid = town->index;
01260 }
01261 SetTownIndex(t, tid);
01262 }
01263 }
01264 }
01265
01266 static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
01267 {
01268 uint z;
01269 Slope tileh = GetTileSlope(tile, &z);
01270
01271 if (tileh == SLOPE_FLAT) return z;
01272 if (IsNormalRoad(tile)) {
01273 Foundation f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
01274 z += ApplyFoundationToSlope(f, &tileh);
01275 return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
01276 } else {
01277 return z + TILE_HEIGHT;
01278 }
01279 }
01280
01281 static Foundation GetFoundation_Road(TileIndex tile, Slope tileh)
01282 {
01283 if (IsNormalRoad(tile)) {
01284 return GetRoadFoundation(tileh, GetAllRoadBits(tile));
01285 } else {
01286 return FlatteningFoundation(tileh);
01287 }
01288 }
01289
01290 static void GetAcceptedCargo_Road(TileIndex tile, AcceptedCargo ac)
01291 {
01292
01293 }
01294
01295 static void AnimateTile_Road(TileIndex tile)
01296 {
01297 if (IsLevelCrossing(tile)) MarkTileDirtyByTile(tile);
01298 }
01299
01300
01301 static const Roadside _town_road_types[][2] = {
01302 { ROADSIDE_GRASS, ROADSIDE_GRASS },
01303 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01304 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01305 { ROADSIDE_TREES, ROADSIDE_TREES },
01306 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
01307 };
01308
01309 static const Roadside _town_road_types_2[][2] = {
01310 { ROADSIDE_GRASS, ROADSIDE_GRASS },
01311 { ROADSIDE_PAVED, ROADSIDE_PAVED },
01312 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
01313 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
01314 { ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
01315 };
01316
01317
01318 static void TileLoop_Road(TileIndex tile)
01319 {
01320 switch (_settings_game.game_creation.landscape) {
01321 case LT_ARCTIC:
01322 if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
01323 ToggleSnow(tile);
01324 MarkTileDirtyByTile(tile);
01325 }
01326 break;
01327
01328 case LT_TROPIC:
01329 if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
01330 ToggleDesert(tile);
01331 MarkTileDirtyByTile(tile);
01332 }
01333 break;
01334 }
01335
01336 if (IsRoadDepot(tile)) return;
01337
01338 const Town *t = ClosestTownFromTile(tile, UINT_MAX);
01339 if (!HasRoadWorks(tile)) {
01340 HouseZonesBits grp = HZB_TOWN_EDGE;
01341
01342 if (t != NULL) {
01343 grp = GetTownRadiusGroup(t, tile);
01344
01345
01346 if (t->road_build_months != 0 &&
01347 (DistanceManhattan(t->xy, tile) < 8 || grp != HZB_TOWN_EDGE) &&
01348 IsNormalRoad(tile) && CountBits(GetAllRoadBits(tile)) > 1 ) {
01349 if (GetFoundationSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && Chance16(1, 40)) {
01350 StartRoadWorks(tile);
01351
01352 SndPlayTileFx(SND_21_JACKHAMMER, tile);
01353 CreateEffectVehicleAbove(
01354 TileX(tile) * TILE_SIZE + 7,
01355 TileY(tile) * TILE_SIZE + 7,
01356 0,
01357 EV_BULLDOZER);
01358 MarkTileDirtyByTile(tile);
01359 return;
01360 }
01361 }
01362 }
01363
01364 {
01365
01366 const Roadside *new_rs = (_settings_game.game_creation.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
01367 Roadside cur_rs = GetRoadside(tile);
01368
01369
01370 if (cur_rs == new_rs[0]) return;
01371
01372
01373 if (cur_rs == new_rs[1]) {
01374 cur_rs = new_rs[0];
01375
01376 } else if (cur_rs == ROADSIDE_BARREN) {
01377 cur_rs = new_rs[1];
01378
01379 } else {
01380 cur_rs = ROADSIDE_BARREN;
01381 }
01382 SetRoadside(tile, cur_rs);
01383 MarkTileDirtyByTile(tile);
01384 }
01385 } else if (IncreaseRoadWorksCounter(tile)) {
01386 TerminateRoadWorks(tile);
01387
01388 if (_settings_game.economy.mod_road_rebuild) {
01389
01390 const RoadBits old_rb = GetAnyRoadBits(tile, ROADTYPE_ROAD);
01391 const RoadBits new_rb = CleanUpRoadBits(tile, old_rb);
01392
01393 if (old_rb != new_rb) {
01394 RemoveRoad(tile, DC_EXEC | DC_AUTO | DC_NO_WATER, (old_rb ^ new_rb), ROADTYPE_ROAD, true);
01395 }
01396 }
01397
01398 MarkTileDirtyByTile(tile);
01399 }
01400 }
01401
01402 static bool ClickTile_Road(TileIndex tile)
01403 {
01404 if (!IsRoadDepot(tile)) return false;
01405
01406 ShowDepotWindow(tile, VEH_ROAD);
01407 return true;
01408 }
01409
01410
01411 static const byte _road_trackbits[16] = {
01412 0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F,
01413 };
01414
01415 static TrackStatus GetTileTrackStatus_Road(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
01416 {
01417 TrackdirBits trackdirbits = TRACKDIR_BIT_NONE;
01418 TrackdirBits red_signals = TRACKDIR_BIT_NONE;
01419 switch (mode) {
01420 case TRANSPORT_RAIL:
01421 if (IsLevelCrossing(tile)) trackdirbits = TrackBitsToTrackdirBits(GetCrossingRailBits(tile));
01422 break;
01423
01424 case TRANSPORT_ROAD:
01425 if ((GetRoadTypes(tile) & sub_mode) == 0) break;
01426 switch (GetRoadTileType(tile)) {
01427 case ROAD_TILE_NORMAL: {
01428 const uint drd_to_multiplier[DRD_END] = { 0x101, 0x100, 0x1, 0x0 };
01429 RoadType rt = (RoadType)FindFirstBit(sub_mode);
01430 RoadBits bits = GetRoadBits(tile, rt);
01431
01432
01433 if (side != INVALID_DIAGDIR && (DiagDirToRoadBits(side) & bits) == 0) break;
01434
01435 uint multiplier = drd_to_multiplier[rt == ROADTYPE_TRAM ? DRD_NONE : GetDisallowedRoadDirections(tile)];
01436 if (!HasRoadWorks(tile)) trackdirbits = (TrackdirBits)(_road_trackbits[bits] * multiplier);
01437 break;
01438 }
01439
01440 case ROAD_TILE_CROSSING: {
01441 Axis axis = GetCrossingRoadAxis(tile);
01442
01443 if (side != INVALID_DIAGDIR && axis != DiagDirToAxis(side)) break;
01444
01445 trackdirbits = TrackBitsToTrackdirBits(AxisToTrackBits(axis));
01446 if (IsCrossingBarred(tile)) red_signals = trackdirbits;
01447 break;
01448 }
01449
01450 default:
01451 case ROAD_TILE_DEPOT: {
01452 DiagDirection dir = GetRoadDepotDirection(tile);
01453
01454 if (side != INVALID_DIAGDIR && side != dir) break;
01455
01456 trackdirbits = TrackBitsToTrackdirBits(DiagDirToDiagTrackBits(dir));
01457 break;
01458 }
01459 }
01460 break;
01461
01462 default: break;
01463 }
01464 return CombineTrackStatus(trackdirbits, red_signals);
01465 }
01466
01467 static const StringID _road_tile_strings[] = {
01468 STR_1814_ROAD,
01469 STR_1814_ROAD,
01470 STR_1814_ROAD,
01471 STR_1815_ROAD_WITH_STREETLIGHTS,
01472 STR_1814_ROAD,
01473 STR_1816_TREE_LINED_ROAD,
01474 STR_1814_ROAD,
01475 STR_1814_ROAD,
01476 };
01477
01478 static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
01479 {
01480 Owner rail_owner = INVALID_OWNER;
01481 Owner road_owner = INVALID_OWNER;
01482 Owner tram_owner = INVALID_OWNER;
01483
01484 switch (GetRoadTileType(tile)) {
01485 case ROAD_TILE_CROSSING: {
01486 td->str = STR_1818_ROAD_RAIL_LEVEL_CROSSING;
01487 RoadTypes rts = GetRoadTypes(tile);
01488 rail_owner = GetTileOwner(tile);
01489 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01490 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01491 break;
01492 }
01493
01494 case ROAD_TILE_DEPOT:
01495 td->str = STR_1817_ROAD_VEHICLE_DEPOT;
01496 road_owner = GetTileOwner(tile);
01497 break;
01498
01499 default: {
01500 RoadTypes rts = GetRoadTypes(tile);
01501 td->str = (HasBit(rts, ROADTYPE_ROAD) ? _road_tile_strings[GetRoadside(tile)] : STR_TRAMWAY);
01502 if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
01503 if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
01504 break;
01505 }
01506 }
01507
01508
01509
01510
01511
01512
01513 Owner first_owner = (road_owner == INVALID_OWNER ? tram_owner : road_owner);
01514 bool mixed_owners = (tram_owner != INVALID_OWNER && tram_owner != first_owner) || (rail_owner != INVALID_OWNER && rail_owner != first_owner);
01515
01516 if (mixed_owners) {
01517
01518 td->owner_type[0] = (rail_owner == INVALID_OWNER ? STR_NULL : STR_RAIL_OWNER);
01519 td->owner[0] = rail_owner;
01520 td->owner_type[1] = (road_owner == INVALID_OWNER ? STR_NULL : STR_ROAD_OWNER);
01521 td->owner[1] = road_owner;
01522 td->owner_type[2] = (tram_owner == INVALID_OWNER ? STR_NULL : STR_TRAM_OWNER);
01523 td->owner[2] = tram_owner;
01524 } else {
01525
01526 td->owner[0] = first_owner;
01527 }
01528 }
01529
01534 static const byte _roadveh_enter_depot_dir[4] = {
01535 TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
01536 };
01537
01538 static VehicleEnterTileStatus VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
01539 {
01540 switch (GetRoadTileType(tile)) {
01541 case ROAD_TILE_CROSSING:
01542 if (v->type == VEH_TRAIN) {
01543
01544 assert(IsCrossingBarred(tile));
01545 }
01546 break;
01547
01548 case ROAD_TILE_DEPOT:
01549 if (v->type == VEH_ROAD &&
01550 v->u.road.frame == RVC_DEPOT_STOP_FRAME &&
01551 _roadveh_enter_depot_dir[GetRoadDepotDirection(tile)] == v->u.road.state) {
01552 v->u.road.state = RVSB_IN_DEPOT;
01553 v->vehstatus |= VS_HIDDEN;
01554 v->direction = ReverseDir(v->direction);
01555 if (v->Next() == NULL) VehicleEnterDepot(v);
01556 v->tile = tile;
01557
01558 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
01559 return VETSB_ENTERED_WORMHOLE;
01560 }
01561 break;
01562
01563 default: break;
01564 }
01565 return VETSB_CONTINUE;
01566 }
01567
01568
01569 static void ChangeTileOwner_Road(TileIndex tile, Owner old_owner, Owner new_owner)
01570 {
01571 if (IsRoadDepot(tile)) {
01572 if (GetTileOwner(tile) == old_owner) {
01573 if (new_owner == INVALID_OWNER) {
01574 DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
01575 } else {
01576 SetTileOwner(tile, new_owner);
01577 }
01578 }
01579 return;
01580 }
01581
01582 for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
01583
01584 if (GetRoadOwner(tile, rt) == old_owner) {
01585 SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
01586 }
01587 }
01588
01589 if (IsLevelCrossing(tile)) {
01590 if (GetTileOwner(tile) == old_owner) {
01591 if (new_owner == INVALID_OWNER) {
01592 DoCommand(tile, 0, GetCrossingRailTrack(tile), DC_EXEC | DC_BANKRUPT, CMD_REMOVE_SINGLE_RAIL);
01593 } else {
01594 SetTileOwner(tile, new_owner);
01595 }
01596 }
01597 }
01598 }
01599
01600 static CommandCost TerraformTile_Road(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
01601 {
01602 if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
01603 switch (GetRoadTileType(tile)) {
01604 case ROAD_TILE_CROSSING:
01605 if (!IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new)) && HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
01606 break;
01607
01608 case ROAD_TILE_DEPOT:
01609 if (AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRoadDepotDirection(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
01610 break;
01611
01612 case ROAD_TILE_NORMAL: {
01613 RoadBits bits = GetAllRoadBits(tile);
01614 RoadBits bits_copy = bits;
01615
01616 if (!CmdFailed(CheckRoadSlope(tileh_new, &bits_copy, ROAD_NONE, ROAD_NONE))) {
01617
01618 if (bits == bits_copy) {
01619 uint z_old;
01620 Slope tileh_old = GetTileSlope(tile, &z_old);
01621
01622
01623 z_old += ApplyFoundationToSlope(GetRoadFoundation(tileh_old, bits), &tileh_old);
01624 z_new += ApplyFoundationToSlope(GetRoadFoundation(tileh_new, bits), &tileh_new);
01625
01626
01627 if ((z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
01628 }
01629 }
01630 break;
01631 }
01632
01633 default: NOT_REACHED();
01634 }
01635 }
01636
01637 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
01638 }
01639
01641 extern const TileTypeProcs _tile_type_road_procs = {
01642 DrawTile_Road,
01643 GetSlopeZ_Road,
01644 ClearTile_Road,
01645 GetAcceptedCargo_Road,
01646 GetTileDesc_Road,
01647 GetTileTrackStatus_Road,
01648 ClickTile_Road,
01649 AnimateTile_Road,
01650 TileLoop_Road,
01651 ChangeTileOwner_Road,
01652 NULL,
01653 VehicleEnter_Road,
01654 GetFoundation_Road,
01655 TerraformTile_Road,
01656 };