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