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