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