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