OpenTTD Source  20240915-master-g3784a3d3d6
tunnelbridge_cmd.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
14 #include "stdafx.h"
15 #include "newgrf_object.h"
16 #include "viewport_func.h"
17 #include "command_func.h"
18 #include "town.h"
19 #include "train.h"
20 #include "ship.h"
21 #include "roadveh.h"
24 #include "newgrf_sound.h"
25 #include "autoslope.h"
26 #include "tunnelbridge_map.h"
27 #include "strings_func.h"
29 #include "clear_func.h"
30 #include "vehicle_func.h"
31 #include "sound_func.h"
32 #include "tunnelbridge.h"
33 #include "cheat_type.h"
34 #include "elrail_func.h"
35 #include "pbs.h"
36 #include "company_base.h"
37 #include "newgrf_railtype.h"
38 #include "newgrf_roadtype.h"
39 #include "object_base.h"
40 #include "water.h"
41 #include "company_gui.h"
42 #include "station_func.h"
43 #include "tunnelbridge_cmd.h"
44 #include "landscape_cmd.h"
45 #include "terraform_cmd.h"
46 
47 #include "table/strings.h"
48 #include "table/bridge_land.h"
49 
50 #include "safeguards.h"
51 
54 
56 static const int BRIDGE_Z_START = 3;
57 
58 
67 void MarkBridgeDirty(TileIndex begin, TileIndex end, DiagDirection direction, uint bridge_height)
68 {
69  TileIndexDiff delta = TileOffsByDiagDir(direction);
70  for (TileIndex t = begin; t != end; t += delta) {
71  MarkTileDirtyByTile(t, bridge_height - TileHeight(t));
72  }
74 }
75 
81 {
83 }
84 
87 {
88  /* First, free sprite table data */
89  for (BridgeType i = 0; i < MAX_BRIDGES; i++) {
90  if (_bridge[i].sprite_table != nullptr) {
91  for (BridgePieces j = BRIDGE_PIECE_NORTH; j < BRIDGE_PIECE_INVALID; j++) free(_bridge[i].sprite_table[j]);
92  free(_bridge[i].sprite_table);
93  }
94  }
95 
96  /* Then, wipe out current bridges */
97  memset(&_bridge, 0, sizeof(_bridge));
98  /* And finally, reinstall default data */
99  memcpy(&_bridge, &_orig_bridge, sizeof(_orig_bridge));
100 }
101 
108 int CalcBridgeLenCostFactor(int length)
109 {
110  if (length < 2) return length;
111 
112  length -= 2;
113  int sum = 2;
114  for (int delta = 1;; delta++) {
115  for (int count = 0; count < delta; count++) {
116  if (length == 0) return sum;
117  sum += delta;
118  length--;
119  }
120  }
121 }
122 
130 {
131  if (tileh == SLOPE_FLAT ||
132  ((tileh == SLOPE_NE || tileh == SLOPE_SW) && axis == AXIS_X) ||
133  ((tileh == SLOPE_NW || tileh == SLOPE_SE) && axis == AXIS_Y)) return FOUNDATION_NONE;
134 
135  return (HasSlopeHighestCorner(tileh) ? InclinedFoundation(axis) : FlatteningFoundation(tileh));
136 }
137 
145 bool HasBridgeFlatRamp(Slope tileh, Axis axis)
146 {
147  ApplyFoundationToSlope(GetBridgeFoundation(tileh, axis), tileh);
148  /* If the foundation slope is flat the bridge has a non-flat ramp and vice versa. */
149  return (tileh != SLOPE_FLAT);
150 }
151 
152 static inline const PalSpriteID *GetBridgeSpriteTable(int index, BridgePieces table)
153 {
154  const BridgeSpec *bridge = GetBridgeSpec(index);
155  assert(table < BRIDGE_PIECE_INVALID);
156  if (bridge->sprite_table == nullptr || bridge->sprite_table[table] == nullptr) {
157  return _bridge_sprite_table[index][table];
158  } else {
159  return bridge->sprite_table[table];
160  }
161 }
162 
163 
173 static CommandCost CheckBridgeSlope(BridgePieces bridge_piece, Axis axis, Slope &tileh, int &z)
174 {
175  assert(bridge_piece == BRIDGE_PIECE_NORTH || bridge_piece == BRIDGE_PIECE_SOUTH);
176 
177  Foundation f = GetBridgeFoundation(tileh, axis);
178  z += ApplyFoundationToSlope(f, tileh);
179 
180  Slope valid_inclined;
181  if (bridge_piece == BRIDGE_PIECE_NORTH) {
182  valid_inclined = (axis == AXIS_X ? SLOPE_NE : SLOPE_NW);
183  } else {
184  valid_inclined = (axis == AXIS_X ? SLOPE_SW : SLOPE_SE);
185  }
186  if ((tileh != SLOPE_FLAT) && (tileh != valid_inclined)) return CMD_ERROR;
187 
188  if (f == FOUNDATION_NONE) return CommandCost();
189 
190  return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
191 }
192 
200 CommandCost CheckBridgeAvailability(BridgeType bridge_type, uint bridge_len, DoCommandFlag flags)
201 {
202  if (flags & DC_QUERY_COST) {
203  if (bridge_len <= _settings_game.construction.max_bridge_length) return CommandCost();
204  return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
205  }
206 
207  if (bridge_type >= MAX_BRIDGES) return CMD_ERROR;
208 
209  const BridgeSpec *b = GetBridgeSpec(bridge_type);
211 
212  uint max = std::min(b->max_length, _settings_game.construction.max_bridge_length);
213 
214  if (b->min_length > bridge_len) return CMD_ERROR;
215  if (bridge_len <= max) return CommandCost();
216  return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
217 }
218 
224 static Money TunnelBridgeClearCost(TileIndex tile, Price base_price)
225 {
226  Money base_cost = _price[base_price];
227 
228  /* Add the cost of the transport that is on the tunnel/bridge. */
229  switch (GetTunnelBridgeTransportType(tile)) {
230  case TRANSPORT_ROAD: {
231  RoadType road_rt = GetRoadTypeRoad(tile);
232  RoadType tram_rt = GetRoadTypeTram(tile);
233 
234  if (road_rt != INVALID_ROADTYPE) {
235  base_cost += 2 * RoadClearCost(road_rt);
236  }
237  if (tram_rt != INVALID_ROADTYPE) {
238  base_cost += 2 * RoadClearCost(tram_rt);
239  }
240  } break;
241 
242  case TRANSPORT_RAIL: base_cost += RailClearCost(GetRailType(tile)); break;
243  /* Aquaducts have their own clear price. */
244  case TRANSPORT_WATER: base_cost = _price[PR_CLEAR_AQUEDUCT]; break;
245  default: break;
246  }
247 
248  return base_cost;
249 }
250 
261 CommandCost CmdBuildBridge(DoCommandFlag flags, TileIndex tile_end, TileIndex tile_start, TransportType transport_type, BridgeType bridge_type, uint8_t road_rail_type)
262 {
263  CompanyID company = _current_company;
264 
265  RailType railtype = INVALID_RAILTYPE;
266  RoadType roadtype = INVALID_ROADTYPE;
267 
268  if (!IsValidTile(tile_start)) return_cmd_error(STR_ERROR_BRIDGE_THROUGH_MAP_BORDER);
269 
270  /* type of bridge */
271  switch (transport_type) {
272  case TRANSPORT_ROAD:
273  roadtype = (RoadType)road_rail_type;
274  if (!ValParamRoadType(roadtype)) return CMD_ERROR;
275  break;
276 
277  case TRANSPORT_RAIL:
278  railtype = (RailType)road_rail_type;
279  if (!ValParamRailType(railtype)) return CMD_ERROR;
280  break;
281 
282  case TRANSPORT_WATER:
283  break;
284 
285  default:
286  /* Airports don't have bridges. */
287  return CMD_ERROR;
288  }
289 
290  if (company == OWNER_DEITY) {
291  if (transport_type != TRANSPORT_ROAD) return CMD_ERROR;
292  const Town *town = CalcClosestTownFromTile(tile_start);
293 
294  company = OWNER_TOWN;
295 
296  /* If we are not within a town, we are not owned by the town */
297  if (town == nullptr || DistanceSquare(tile_start, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
298  company = OWNER_NONE;
299  }
300  }
301 
302  if (tile_start == tile_end) {
303  return_cmd_error(STR_ERROR_CAN_T_START_AND_END_ON);
304  }
305 
306  Axis direction;
307  if (TileX(tile_start) == TileX(tile_end)) {
308  direction = AXIS_Y;
309  } else if (TileY(tile_start) == TileY(tile_end)) {
310  direction = AXIS_X;
311  } else {
312  return_cmd_error(STR_ERROR_START_AND_END_MUST_BE_IN);
313  }
314 
315  if (tile_end < tile_start) Swap(tile_start, tile_end);
316 
317  uint bridge_len = GetTunnelBridgeLength(tile_start, tile_end);
318  if (transport_type != TRANSPORT_WATER) {
319  /* set and test bridge length, availability */
320  CommandCost ret = CheckBridgeAvailability(bridge_type, bridge_len, flags);
321  if (ret.Failed()) return ret;
322  } else {
323  if (bridge_len > _settings_game.construction.max_bridge_length) return_cmd_error(STR_ERROR_BRIDGE_TOO_LONG);
324  }
325  bridge_len += 2; // begin and end tiles/ramps
326 
327  auto [tileh_start, z_start] = GetTileSlopeZ(tile_start);
328  auto [tileh_end, z_end] = GetTileSlopeZ(tile_end);
329  bool pbs_reservation = false;
330 
331  CommandCost terraform_cost_north = CheckBridgeSlope(BRIDGE_PIECE_NORTH, direction, tileh_start, z_start);
332  CommandCost terraform_cost_south = CheckBridgeSlope(BRIDGE_PIECE_SOUTH, direction, tileh_end, z_end);
333 
334  /* Aqueducts can't be built of flat land. */
335  if (transport_type == TRANSPORT_WATER && (tileh_start == SLOPE_FLAT || tileh_end == SLOPE_FLAT)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
336  if (z_start != z_end) return_cmd_error(STR_ERROR_BRIDGEHEADS_NOT_SAME_HEIGHT);
337 
339  Owner owner;
340  bool is_new_owner;
341  RoadType road_rt = INVALID_ROADTYPE;
342  RoadType tram_rt = INVALID_ROADTYPE;
343  if (IsBridgeTile(tile_start) && IsBridgeTile(tile_end) &&
344  GetOtherBridgeEnd(tile_start) == tile_end &&
345  GetTunnelBridgeTransportType(tile_start) == transport_type) {
346  /* Replace a current bridge. */
347 
348  switch (transport_type) {
349  case TRANSPORT_RAIL:
350  /* Keep the reservation, the path stays valid. */
351  pbs_reservation = HasTunnelBridgeReservation(tile_start);
352  break;
353 
354  case TRANSPORT_ROAD:
355  /* Do not remove road types when upgrading a bridge */
356  road_rt = GetRoadTypeRoad(tile_start);
357  tram_rt = GetRoadTypeTram(tile_start);
358  break;
359 
360  default: break;
361  }
362 
363  /* If this is a railway bridge, make sure the railtypes match. */
364  if (transport_type == TRANSPORT_RAIL && GetRailType(tile_start) != railtype) {
365  return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
366  }
367 
368  /* If this is a road bridge, make sure the roadtype matches. */
369  if (transport_type == TRANSPORT_ROAD) {
370  RoadType existing_rt = RoadTypeIsRoad(roadtype) ? road_rt : tram_rt;
371  if (existing_rt != roadtype && existing_rt != INVALID_ROADTYPE) {
372  return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
373  }
374  }
375 
376  /* Do not replace town bridges with lower speed bridges, unless in scenario editor. */
377  if (!(flags & DC_QUERY_COST) && IsTileOwner(tile_start, OWNER_TOWN) &&
378  GetBridgeSpec(bridge_type)->speed < GetBridgeSpec(GetBridgeType(tile_start))->speed &&
379  _game_mode != GM_EDITOR) {
380  Town *t = ClosestTownFromTile(tile_start, UINT_MAX);
381 
382  if (t == nullptr) {
383  return CMD_ERROR;
384  } else {
385  SetDParam(0, t->index);
386  return_cmd_error(STR_ERROR_LOCAL_AUTHORITY_REFUSES_TO_ALLOW_THIS);
387  }
388  }
389 
390  /* Do not replace the bridge with the same bridge type. */
391  if (!(flags & DC_QUERY_COST) && (bridge_type == GetBridgeType(tile_start)) && (transport_type != TRANSPORT_ROAD || road_rt == roadtype || tram_rt == roadtype)) {
392  return_cmd_error(STR_ERROR_ALREADY_BUILT);
393  }
394 
395  /* Do not allow replacing another company's bridges. */
396  if (!IsTileOwner(tile_start, company) && !IsTileOwner(tile_start, OWNER_TOWN) && !IsTileOwner(tile_start, OWNER_NONE)) {
397  return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER);
398  }
399 
400  /* The cost of clearing the current bridge. */
401  cost.AddCost(bridge_len * TunnelBridgeClearCost(tile_start, PR_CLEAR_BRIDGE));
402  owner = GetTileOwner(tile_start);
403 
404  /* If bridge belonged to bankrupt company, it has a new owner now */
405  is_new_owner = (owner == OWNER_NONE);
406  if (is_new_owner) owner = company;
407  } else {
408  /* Build a new bridge. */
409 
410  bool allow_on_slopes = (_settings_game.construction.build_on_slopes && transport_type != TRANSPORT_WATER);
411 
412  /* Try and clear the start landscape */
413  CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile_start);
414  if (ret.Failed()) return ret;
415  cost = ret;
416 
417  if (terraform_cost_north.Failed() || (terraform_cost_north.GetCost() != 0 && !allow_on_slopes)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
418  cost.AddCost(terraform_cost_north);
419 
420  /* Try and clear the end landscape */
421  ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile_end);
422  if (ret.Failed()) return ret;
423  cost.AddCost(ret);
424 
425  /* false - end tile slope check */
426  if (terraform_cost_south.Failed() || (terraform_cost_south.GetCost() != 0 && !allow_on_slopes)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
427  cost.AddCost(terraform_cost_south);
428 
429  const TileIndex heads[] = {tile_start, tile_end};
430  for (int i = 0; i < 2; i++) {
431  if (IsBridgeAbove(heads[i])) {
432  TileIndex north_head = GetNorthernBridgeEnd(heads[i]);
433 
434  if (direction == GetBridgeAxis(heads[i])) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
435 
436  if (z_start + 1 == GetBridgeHeight(north_head)) {
437  return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
438  }
439  }
440  }
441 
442  TileIndexDiff delta = (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
443  for (TileIndex tile = tile_start + delta; tile != tile_end; tile += delta) {
444  if (GetTileMaxZ(tile) > z_start) return_cmd_error(STR_ERROR_BRIDGE_TOO_LOW_FOR_TERRAIN);
445 
446  if (z_start >= (GetTileZ(tile) + _settings_game.construction.max_bridge_height)) {
447  /*
448  * Disallow too high bridges.
449  * Properly rendering a map where very high bridges (might) exist is expensive.
450  * See http://www.tt-forums.net/viewtopic.php?f=33&t=40844&start=980#p1131762
451  * for a detailed discussion. z_start here is one heightlevel below the bridge level.
452  */
453  return_cmd_error(STR_ERROR_BRIDGE_TOO_HIGH_FOR_TERRAIN);
454  }
455 
456  if (IsBridgeAbove(tile)) {
457  /* Disallow crossing bridges for the time being */
458  return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
459  }
460 
461  switch (GetTileType(tile)) {
462  case MP_WATER:
463  if (!IsWater(tile) && !IsCoast(tile)) goto not_valid_below;
464  break;
465 
466  case MP_RAILWAY:
467  if (!IsPlainRail(tile)) goto not_valid_below;
468  break;
469 
470  case MP_ROAD:
471  if (IsRoadDepot(tile)) goto not_valid_below;
472  break;
473 
474  case MP_TUNNELBRIDGE:
475  if (IsTunnel(tile)) break;
476  if (direction == DiagDirToAxis(GetTunnelBridgeDirection(tile))) goto not_valid_below;
477  if (z_start < GetBridgeHeight(tile)) goto not_valid_below;
478  break;
479 
480  case MP_OBJECT: {
481  const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
482  if ((spec->flags & OBJECT_FLAG_ALLOW_UNDER_BRIDGE) == 0) goto not_valid_below;
483  if (GetTileMaxZ(tile) + spec->height > z_start) goto not_valid_below;
484  break;
485  }
486 
487  case MP_CLEAR:
488  break;
489 
490  default:
491  not_valid_below:;
492  /* try and clear the middle landscape */
493  ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile);
494  if (ret.Failed()) return ret;
495  cost.AddCost(ret);
496  break;
497  }
498 
499  if (flags & DC_EXEC) {
500  /* We do this here because when replacing a bridge with another
501  * type calling SetBridgeMiddle isn't needed. After all, the
502  * tile already has the has_bridge_above bits set. */
503  SetBridgeMiddle(tile, direction);
504  }
505  }
506 
507  owner = company;
508  is_new_owner = true;
509  }
510 
511  bool hasroad = road_rt != INVALID_ROADTYPE;
512  bool hastram = tram_rt != INVALID_ROADTYPE;
513  if (transport_type == TRANSPORT_ROAD) {
514  if (RoadTypeIsRoad(roadtype)) road_rt = roadtype;
515  if (RoadTypeIsTram(roadtype)) tram_rt = roadtype;
516  }
517 
518  /* do the drill? */
519  if (flags & DC_EXEC) {
520  DiagDirection dir = AxisToDiagDir(direction);
521 
522  Company *c = Company::GetIfValid(company);
523  switch (transport_type) {
524  case TRANSPORT_RAIL:
525  /* Add to company infrastructure count if required. */
526  if (is_new_owner && c != nullptr) c->infrastructure.rail[railtype] += bridge_len * TUNNELBRIDGE_TRACKBIT_FACTOR;
527  MakeRailBridgeRamp(tile_start, owner, bridge_type, dir, railtype);
528  MakeRailBridgeRamp(tile_end, owner, bridge_type, ReverseDiagDir(dir), railtype);
529  SetTunnelBridgeReservation(tile_start, pbs_reservation);
530  SetTunnelBridgeReservation(tile_end, pbs_reservation);
531  break;
532 
533  case TRANSPORT_ROAD: {
534  if (is_new_owner) {
535  /* Also give unowned present roadtypes to new owner */
536  if (hasroad && GetRoadOwner(tile_start, RTT_ROAD) == OWNER_NONE) hasroad = false;
537  if (hastram && GetRoadOwner(tile_start, RTT_TRAM) == OWNER_NONE) hastram = false;
538  }
539  if (c != nullptr) {
540  /* Add all new road types to the company infrastructure counter. */
541  if (!hasroad && road_rt != INVALID_ROADTYPE) {
542  /* A full diagonal road tile has two road bits. */
543  c->infrastructure.road[road_rt] += bridge_len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR;
544  }
545  if (!hastram && tram_rt != INVALID_ROADTYPE) {
546  /* A full diagonal road tile has two road bits. */
547  c->infrastructure.road[tram_rt] += bridge_len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR;
548  }
549  }
550  Owner owner_road = hasroad ? GetRoadOwner(tile_start, RTT_ROAD) : company;
551  Owner owner_tram = hastram ? GetRoadOwner(tile_start, RTT_TRAM) : company;
552  MakeRoadBridgeRamp(tile_start, owner, owner_road, owner_tram, bridge_type, dir, road_rt, tram_rt);
553  MakeRoadBridgeRamp(tile_end, owner, owner_road, owner_tram, bridge_type, ReverseDiagDir(dir), road_rt, tram_rt);
554  break;
555  }
556 
557  case TRANSPORT_WATER:
558  if (is_new_owner && c != nullptr) c->infrastructure.water += bridge_len * TUNNELBRIDGE_TRACKBIT_FACTOR;
559  MakeAqueductBridgeRamp(tile_start, owner, dir);
560  MakeAqueductBridgeRamp(tile_end, owner, ReverseDiagDir(dir));
561  CheckForDockingTile(tile_start);
562  CheckForDockingTile(tile_end);
563  break;
564 
565  default:
566  NOT_REACHED();
567  }
568 
569  /* Mark all tiles dirty */
570  MarkBridgeDirty(tile_start, tile_end, AxisToDiagDir(direction), z_start);
572  }
573 
574  if ((flags & DC_EXEC) && transport_type == TRANSPORT_RAIL) {
575  Track track = AxisToTrack(direction);
576  AddSideToSignalBuffer(tile_start, INVALID_DIAGDIR, company);
577  YapfNotifyTrackLayoutChange(tile_start, track);
578  }
579 
580  /* Human players that build bridges get a selection to choose from (DC_QUERY_COST)
581  * It's unnecessary to execute this command every time for every bridge.
582  * So it is done only for humans and cost is computed in bridge_gui.cpp.
583  * For (non-spectated) AI, Towns this has to be of course calculated. */
584  Company *c = Company::GetIfValid(company);
585  if (!(flags & DC_QUERY_COST) || (c != nullptr && c->is_ai && company != _local_company)) {
586  switch (transport_type) {
587  case TRANSPORT_ROAD:
588  if (road_rt != INVALID_ROADTYPE) {
589  cost.AddCost(bridge_len * 2 * RoadBuildCost(road_rt));
590  }
591  if (tram_rt != INVALID_ROADTYPE) {
592  cost.AddCost(bridge_len * 2 * RoadBuildCost(tram_rt));
593  }
594  break;
595 
596  case TRANSPORT_RAIL: cost.AddCost(bridge_len * RailBuildCost(railtype)); break;
597  default: break;
598  }
599 
600  if (c != nullptr) bridge_len = CalcBridgeLenCostFactor(bridge_len);
601 
602  if (transport_type != TRANSPORT_WATER) {
603  cost.AddCost((int64_t)bridge_len * _price[PR_BUILD_BRIDGE] * GetBridgeSpec(bridge_type)->price >> 8);
604  } else {
605  /* Aqueducts use a separate base cost. */
606  cost.AddCost((int64_t)bridge_len * _price[PR_BUILD_AQUEDUCT]);
607  }
608 
609  }
610 
611  return cost;
612 }
613 
614 
623 CommandCost CmdBuildTunnel(DoCommandFlag flags, TileIndex start_tile, TransportType transport_type, uint8_t road_rail_type)
624 {
625  CompanyID company = _current_company;
626 
627  RailType railtype = INVALID_RAILTYPE;
628  RoadType roadtype = INVALID_ROADTYPE;
630  switch (transport_type) {
631  case TRANSPORT_RAIL:
632  railtype = (RailType)road_rail_type;
633  if (!ValParamRailType(railtype)) return CMD_ERROR;
634  break;
635 
636  case TRANSPORT_ROAD:
637  roadtype = (RoadType)road_rail_type;
638  if (!ValParamRoadType(roadtype)) return CMD_ERROR;
639  break;
640 
641  default: return CMD_ERROR;
642  }
643 
644  if (company == OWNER_DEITY) {
645  if (transport_type != TRANSPORT_ROAD) return CMD_ERROR;
646  const Town *town = CalcClosestTownFromTile(start_tile);
647 
648  company = OWNER_TOWN;
649 
650  /* If we are not within a town, we are not owned by the town */
651  if (town == nullptr || DistanceSquare(start_tile, town->xy) > town->cache.squared_town_zone_radius[HZB_TOWN_EDGE]) {
652  company = OWNER_NONE;
653  }
654  }
655 
656  auto [start_tileh, start_z] = GetTileSlopeZ(start_tile);
657  DiagDirection direction = GetInclinedSlopeDirection(start_tileh);
658  if (direction == INVALID_DIAGDIR) return_cmd_error(STR_ERROR_SITE_UNSUITABLE_FOR_TUNNEL);
659 
660  if (HasTileWaterGround(start_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
661 
662  CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, start_tile);
663  if (ret.Failed()) return ret;
664 
665  /* XXX - do NOT change 'ret' in the loop, as it is used as the price
666  * for the clearing of the entrance of the tunnel. Assigning it to
667  * cost before the loop will yield different costs depending on start-
668  * position, because of increased-cost-by-length: 'cost += cost >> 3' */
669 
670  TileIndexDiff delta = TileOffsByDiagDir(direction);
671  DiagDirection tunnel_in_way_dir;
672  if (DiagDirToAxis(direction) == AXIS_Y) {
673  tunnel_in_way_dir = (TileX(start_tile) < (Map::MaxX() / 2)) ? DIAGDIR_SW : DIAGDIR_NE;
674  } else {
675  tunnel_in_way_dir = (TileY(start_tile) < (Map::MaxX() / 2)) ? DIAGDIR_SE : DIAGDIR_NW;
676  }
677 
678  TileIndex end_tile = start_tile;
679 
680  /* Tile shift coefficient. Will decrease for very long tunnels to avoid exponential growth of price*/
681  int tiles_coef = 3;
682  /* Number of tiles from start of tunnel */
683  int tiles = 0;
684  /* Number of tiles at which the cost increase coefficient per tile is halved */
685  int tiles_bump = 25;
686 
688  Slope end_tileh;
689  int end_z;
690  for (;;) {
691  end_tile += delta;
692  if (!IsValidTile(end_tile)) return_cmd_error(STR_ERROR_TUNNEL_THROUGH_MAP_BORDER);
693  std::tie(end_tileh, end_z) = GetTileSlopeZ(end_tile);
694 
695  if (start_z == end_z) break;
696 
697  if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) {
698  return_cmd_error(STR_ERROR_ANOTHER_TUNNEL_IN_THE_WAY);
699  }
700 
701  tiles++;
702  if (tiles == tiles_bump) {
703  tiles_coef++;
704  tiles_bump *= 2;
705  }
706 
707  cost.AddCost(_price[PR_BUILD_TUNNEL]);
708  cost.AddCost(cost.GetCost() >> tiles_coef); // add a multiplier for longer tunnels
709  }
710 
711  /* Add the cost of the entrance */
712  cost.AddCost(_price[PR_BUILD_TUNNEL]);
713  cost.AddCost(ret);
714 
715  /* if the command fails from here on we want the end tile to be highlighted */
716  _build_tunnel_endtile = end_tile;
717 
718  if (tiles > _settings_game.construction.max_tunnel_length) return_cmd_error(STR_ERROR_TUNNEL_TOO_LONG);
719 
720  if (HasTileWaterGround(end_tile)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
721 
722  /* Clear the tile in any case */
723  ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, end_tile);
724  if (ret.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
725  cost.AddCost(ret);
726 
727  /* slope of end tile must be complementary to the slope of the start tile */
728  if (end_tileh != ComplementSlope(start_tileh)) {
729  /* Mark the tile as already cleared for the terraform command.
730  * Do this for all tiles (like trees), not only objects. */
731  ClearedObjectArea *coa = FindClearedObject(end_tile);
732  if (coa == nullptr) {
733  coa = &_cleared_object_areas.emplace_back(ClearedObjectArea{ end_tile, TileArea(end_tile, 1, 1) });
734  }
735 
736  /* Hide the tile from the terraforming command */
737  TileIndex old_first_tile = coa->first_tile;
738  coa->first_tile = INVALID_TILE;
739 
740  /* CMD_TERRAFORM_LAND may append further items to _cleared_object_areas,
741  * however it will never erase or re-order existing items.
742  * _cleared_object_areas is a value-type self-resizing vector, therefore appending items
743  * may result in a backing-store re-allocation, which would invalidate the coa pointer.
744  * The index of the coa pointer into the _cleared_object_areas vector remains valid,
745  * and can be used safely after the CMD_TERRAFORM_LAND operation.
746  * Deliberately clear the coa pointer to avoid leaving dangling pointers which could
747  * inadvertently be dereferenced.
748  */
749  ClearedObjectArea *begin = _cleared_object_areas.data();
750  assert(coa >= begin && coa < begin + _cleared_object_areas.size());
751  size_t coa_index = coa - begin;
752  assert(coa_index < UINT_MAX); // more than 2**32 cleared areas would be a bug in itself
753  coa = nullptr;
754 
755  ret = std::get<0>(Command<CMD_TERRAFORM_LAND>::Do(flags, end_tile, end_tileh & start_tileh, false));
756  _cleared_object_areas[(uint)coa_index].first_tile = old_first_tile;
757  if (ret.Failed()) return_cmd_error(STR_ERROR_UNABLE_TO_EXCAVATE_LAND);
758  cost.AddCost(ret);
759  }
760  cost.AddCost(_price[PR_BUILD_TUNNEL]);
761 
762  /* Pay for the rail/road in the tunnel including entrances */
763  switch (transport_type) {
764  case TRANSPORT_ROAD: cost.AddCost((tiles + 2) * RoadBuildCost(roadtype) * 2); break;
765  case TRANSPORT_RAIL: cost.AddCost((tiles + 2) * RailBuildCost(railtype)); break;
766  default: NOT_REACHED();
767  }
768 
769  if (flags & DC_EXEC) {
770  Company *c = Company::GetIfValid(company);
771  uint num_pieces = (tiles + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
772  if (transport_type == TRANSPORT_RAIL) {
773  if (c != nullptr) c->infrastructure.rail[railtype] += num_pieces;
774  MakeRailTunnel(start_tile, company, direction, railtype);
775  MakeRailTunnel(end_tile, company, ReverseDiagDir(direction), railtype);
776  AddSideToSignalBuffer(start_tile, INVALID_DIAGDIR, company);
777  YapfNotifyTrackLayoutChange(start_tile, DiagDirToDiagTrack(direction));
778  } else {
779  if (c != nullptr) c->infrastructure.road[roadtype] += num_pieces * 2; // A full diagonal road has two road bits.
780  RoadType road_rt = RoadTypeIsRoad(roadtype) ? roadtype : INVALID_ROADTYPE;
781  RoadType tram_rt = RoadTypeIsTram(roadtype) ? roadtype : INVALID_ROADTYPE;
782  MakeRoadTunnel(start_tile, company, direction, road_rt, tram_rt);
783  MakeRoadTunnel(end_tile, company, ReverseDiagDir(direction), road_rt, tram_rt);
784  }
786  }
787 
788  return cost;
789 }
790 
791 
798 {
799  /* Floods can remove anything as well as the scenario editor */
800  if (_current_company == OWNER_WATER || _game_mode == GM_EDITOR) return CommandCost();
801 
802  switch (GetTunnelBridgeTransportType(tile)) {
803  case TRANSPORT_ROAD: {
804  RoadType road_rt = GetRoadTypeRoad(tile);
805  RoadType tram_rt = GetRoadTypeTram(tile);
806  Owner road_owner = _current_company;
807  Owner tram_owner = _current_company;
808 
809  if (road_rt != INVALID_ROADTYPE) road_owner = GetRoadOwner(tile, RTT_ROAD);
810  if (tram_rt != INVALID_ROADTYPE) tram_owner = GetRoadOwner(tile, RTT_TRAM);
811 
812  /* We can remove unowned road and if the town allows it */
814  /* Town does not allow */
815  return CheckTileOwnership(tile);
816  }
817  if (road_owner == OWNER_NONE || road_owner == OWNER_TOWN) road_owner = _current_company;
818  if (tram_owner == OWNER_NONE) tram_owner = _current_company;
819 
820  CommandCost ret = CheckOwnership(road_owner, tile);
821  if (ret.Succeeded()) ret = CheckOwnership(tram_owner, tile);
822  return ret;
823  }
824 
825  case TRANSPORT_RAIL:
826  return CheckOwnership(GetTileOwner(tile));
827 
828  case TRANSPORT_WATER: {
829  /* Always allow to remove aqueducts without owner. */
830  Owner aqueduct_owner = GetTileOwner(tile);
831  if (aqueduct_owner == OWNER_NONE) aqueduct_owner = _current_company;
832  return CheckOwnership(aqueduct_owner);
833  }
834 
835  default: NOT_REACHED();
836  }
837 }
838 
846 {
848  if (ret.Failed()) return ret;
849 
850  TileIndex endtile = GetOtherTunnelEnd(tile);
851 
852  ret = TunnelBridgeIsFree(tile, endtile);
853  if (ret.Failed()) return ret;
854 
855  _build_tunnel_endtile = endtile;
856 
857  Town *t = nullptr;
858  if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
859  t = ClosestTownFromTile(tile, UINT_MAX); // town penalty rating
860 
861  /* Check if you are allowed to remove the tunnel owned by a town
862  * Removal depends on difficulty settings */
863  ret = CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE);
864  if (ret.Failed()) return ret;
865  }
866 
867  /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
868  * you have a "Poor" (0) town rating */
869  if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
871  }
872 
873  Money base_cost = TunnelBridgeClearCost(tile, PR_CLEAR_TUNNEL);
874  uint len = GetTunnelBridgeLength(tile, endtile) + 2; // Don't forget the end tiles.
875 
876  if (flags & DC_EXEC) {
878  /* We first need to request values before calling DoClearSquare */
880  Track track = DiagDirToDiagTrack(dir);
881  Owner owner = GetTileOwner(tile);
882 
883  Train *v = nullptr;
884  if (HasTunnelBridgeReservation(tile)) {
885  v = GetTrainForReservation(tile, track);
886  if (v != nullptr) FreeTrainTrackReservation(v);
887  }
888 
889  if (Company::IsValidID(owner)) {
890  Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
892  }
893 
894  DoClearSquare(tile);
895  DoClearSquare(endtile);
896 
897  /* cannot use INVALID_DIAGDIR for signal update because the tunnel doesn't exist anymore */
898  AddSideToSignalBuffer(tile, ReverseDiagDir(dir), owner);
899  AddSideToSignalBuffer(endtile, dir, owner);
900 
901  YapfNotifyTrackLayoutChange(tile, track);
902  YapfNotifyTrackLayoutChange(endtile, track);
903 
904  if (v != nullptr) TryPathReserve(v);
905  } else {
906  /* A full diagonal road tile has two road bits. */
907  UpdateCompanyRoadInfrastructure(GetRoadTypeRoad(tile), GetRoadOwner(tile, RTT_ROAD), -(int)(len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR));
908  UpdateCompanyRoadInfrastructure(GetRoadTypeTram(tile), GetRoadOwner(tile, RTT_TRAM), -(int)(len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR));
909 
910  DoClearSquare(tile);
911  DoClearSquare(endtile);
912  }
913  }
914 
915  return CommandCost(EXPENSES_CONSTRUCTION, len * base_cost);
916 }
917 
918 
926 {
928  if (ret.Failed()) return ret;
929 
930  TileIndex endtile = GetOtherBridgeEnd(tile);
931 
932  ret = TunnelBridgeIsFree(tile, endtile);
933  if (ret.Failed()) return ret;
934 
935  DiagDirection direction = GetTunnelBridgeDirection(tile);
936  TileIndexDiff delta = TileOffsByDiagDir(direction);
937 
938  Town *t = nullptr;
939  if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
940  t = ClosestTownFromTile(tile, UINT_MAX); // town penalty rating
941 
942  /* Check if you are allowed to remove the bridge owned by a town
943  * Removal depends on difficulty settings */
944  ret = CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE);
945  if (ret.Failed()) return ret;
946  }
947 
948  /* checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
949  * you have a "Poor" (0) town rating */
950  if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
952  }
953 
954  Money base_cost = TunnelBridgeClearCost(tile, PR_CLEAR_BRIDGE);
955  uint len = GetTunnelBridgeLength(tile, endtile) + 2; // Don't forget the end tiles.
956 
957  if (flags & DC_EXEC) {
958  /* read this value before actual removal of bridge */
959  bool rail = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL;
960  Owner owner = GetTileOwner(tile);
961  int height = GetBridgeHeight(tile);
962  Train *v = nullptr;
963 
964  if (rail && HasTunnelBridgeReservation(tile)) {
965  v = GetTrainForReservation(tile, DiagDirToDiagTrack(direction));
966  if (v != nullptr) FreeTrainTrackReservation(v);
967  }
968 
969  /* Update company infrastructure counts. */
970  if (rail) {
971  if (Company::IsValidID(owner)) Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
972  } else if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) {
973  /* A full diagonal road tile has two road bits. */
974  UpdateCompanyRoadInfrastructure(GetRoadTypeRoad(tile), GetRoadOwner(tile, RTT_ROAD), -(int)(len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR));
975  UpdateCompanyRoadInfrastructure(GetRoadTypeTram(tile), GetRoadOwner(tile, RTT_TRAM), -(int)(len * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR));
976  } else { // Aqueduct
977  if (Company::IsValidID(owner)) Company::Get(owner)->infrastructure.water -= len * TUNNELBRIDGE_TRACKBIT_FACTOR;
978  }
980 
981  DoClearSquare(tile);
982  DoClearSquare(endtile);
983 
984  for (TileIndex c = tile + delta; c != endtile; c += delta) {
985  /* do not let trees appear from 'nowhere' after removing bridge */
986  if (IsNormalRoadTile(c) && GetRoadside(c) == ROADSIDE_TREES) {
987  int minz = GetTileMaxZ(c) + 3;
988  if (height < minz) SetRoadside(c, ROADSIDE_PAVED);
989  }
991  MarkTileDirtyByTile(c, height - TileHeight(c));
992  }
993 
994  if (rail) {
995  /* cannot use INVALID_DIAGDIR for signal update because the bridge doesn't exist anymore */
996  AddSideToSignalBuffer(tile, ReverseDiagDir(direction), owner);
997  AddSideToSignalBuffer(endtile, direction, owner);
998 
999  Track track = DiagDirToDiagTrack(direction);
1000  YapfNotifyTrackLayoutChange(tile, track);
1001  YapfNotifyTrackLayoutChange(endtile, track);
1002 
1003  if (v != nullptr) TryPathReserve(v, true);
1004  }
1005  }
1006 
1007  return CommandCost(EXPENSES_CONSTRUCTION, len * base_cost);
1008 }
1009 
1017 {
1018  if (IsTunnel(tile)) {
1019  if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_TUNNEL_FIRST);
1020  return DoClearTunnel(tile, flags);
1021  } else { // IsBridge(tile)
1022  if (flags & DC_AUTO) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
1023  return DoClearBridge(tile, flags);
1024  }
1025 }
1026 
1037 static inline void DrawPillar(const PalSpriteID *psid, int x, int y, int z, int w, int h, const SubSprite *subsprite)
1038 {
1039  static const int PILLAR_Z_OFFSET = TILE_HEIGHT - BRIDGE_Z_START;
1040  AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, w, h, BB_HEIGHT_UNDER_BRIDGE - PILLAR_Z_OFFSET, z, IsTransparencySet(TO_BRIDGES), 0, 0, -PILLAR_Z_OFFSET, subsprite);
1041 }
1042 
1054 static int DrawPillarColumn(int z_bottom, int z_top, const PalSpriteID *psid, int x, int y, int w, int h)
1055 {
1056  int cur_z;
1057  for (cur_z = z_top; cur_z >= z_bottom; cur_z -= TILE_HEIGHT) {
1058  DrawPillar(psid, x, y, cur_z, w, h, nullptr);
1059  }
1060  return cur_z;
1061 }
1062 
1074 static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo *ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge)
1075 {
1076  static const int bounding_box_size[2] = {16, 2};
1077  static const int back_pillar_offset[2] = { 0, 9};
1078 
1079  static const int INF = 1000;
1080  static const SubSprite half_pillar_sub_sprite[2][2] = {
1081  { { -14, -INF, INF, INF }, { -INF, -INF, -15, INF } }, // X axis, north and south
1082  { { -INF, -INF, 15, INF }, { 16, -INF, INF, INF } }, // Y axis, north and south
1083  };
1084 
1085  if (psid->sprite == 0) return;
1086 
1087  /* Determine ground height under pillars */
1088  DiagDirection south_dir = AxisToDiagDir(axis);
1089  int z_front_north = ti->z;
1090  int z_back_north = ti->z;
1091  int z_front_south = ti->z;
1092  int z_back_south = ti->z;
1093  GetSlopePixelZOnEdge(ti->tileh, south_dir, z_front_south, z_back_south);
1094  GetSlopePixelZOnEdge(ti->tileh, ReverseDiagDir(south_dir), z_front_north, z_back_north);
1095 
1096  /* Shared height of pillars */
1097  int z_front = std::max(z_front_north, z_front_south);
1098  int z_back = std::max(z_back_north, z_back_south);
1099 
1100  /* x and y size of bounding-box of pillars */
1101  int w = bounding_box_size[axis];
1102  int h = bounding_box_size[OtherAxis(axis)];
1103  /* sprite position of back facing pillar */
1104  int x_back = x - back_pillar_offset[axis];
1105  int y_back = y - back_pillar_offset[OtherAxis(axis)];
1106 
1107  /* Draw front pillars */
1108  int bottom_z = DrawPillarColumn(z_front, z_bridge, psid, x, y, w, h);
1109  if (z_front_north < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
1110  if (z_front_south < z_front) DrawPillar(psid, x, y, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
1111 
1112  /* Draw back pillars, skip top two parts, which are hidden by the bridge */
1113  int z_bridge_back = z_bridge - 2 * (int)TILE_HEIGHT;
1114  if (drawfarpillar && (z_back_north <= z_bridge_back || z_back_south <= z_bridge_back)) {
1115  bottom_z = DrawPillarColumn(z_back, z_bridge_back, psid, x_back, y_back, w, h);
1116  if (z_back_north < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][0]);
1117  if (z_back_south < z_back) DrawPillar(psid, x_back, y_back, bottom_z, w, h, &half_pillar_sub_sprite[axis][1]);
1118  }
1119 }
1120 
1130 static void GetBridgeRoadCatenary(const RoadTypeInfo *rti, TileIndex head_tile, int offset, bool head, SpriteID &spr_back, SpriteID &spr_front)
1131 {
1132  static const SpriteID back_offsets[6] = { 95, 96, 99, 102, 100, 101 };
1133  static const SpriteID front_offsets[6] = { 97, 98, 103, 106, 104, 105 };
1134 
1135  /* Simplified from DrawRoadTypeCatenary() to remove all the special cases required for regular ground road */
1136  spr_back = GetCustomRoadSprite(rti, head_tile, ROTSG_CATENARY_BACK, head ? TCX_NORMAL : TCX_ON_BRIDGE);
1137  spr_front = GetCustomRoadSprite(rti, head_tile, ROTSG_CATENARY_FRONT, head ? TCX_NORMAL : TCX_ON_BRIDGE);
1138  if (spr_back == 0 && spr_front == 0) {
1139  spr_back = SPR_TRAMWAY_BASE + back_offsets[offset];
1140  spr_front = SPR_TRAMWAY_BASE + front_offsets[offset];
1141  } else {
1142  if (spr_back != 0) spr_back += 23 + offset;
1143  if (spr_front != 0) spr_front += 23 + offset;
1144  }
1145 }
1146 
1156 static void DrawBridgeRoadBits(TileIndex head_tile, int x, int y, int z, int offset, bool head)
1157 {
1158  RoadType road_rt = GetRoadTypeRoad(head_tile);
1159  RoadType tram_rt = GetRoadTypeTram(head_tile);
1160  const RoadTypeInfo *road_rti = road_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(road_rt);
1161  const RoadTypeInfo *tram_rti = tram_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(tram_rt);
1162 
1163  SpriteID seq_back[4] = { 0 };
1164  bool trans_back[4] = { false };
1165  SpriteID seq_front[4] = { 0 };
1166  bool trans_front[4] = { false };
1167 
1168  static const SpriteID overlay_offsets[6] = { 0, 1, 11, 12, 13, 14 };
1169  if (head || !IsInvisibilitySet(TO_BRIDGES)) {
1170  /* Road underlay takes precedence over tram */
1171  trans_back[0] = !head && IsTransparencySet(TO_BRIDGES);
1172  if (road_rti != nullptr) {
1173  if (road_rti->UsesOverlay()) {
1174  seq_back[0] = GetCustomRoadSprite(road_rti, head_tile, ROTSG_BRIDGE, head ? TCX_NORMAL : TCX_ON_BRIDGE) + offset;
1175  }
1176  } else if (tram_rti != nullptr) {
1177  if (tram_rti->UsesOverlay()) {
1178  seq_back[0] = GetCustomRoadSprite(tram_rti, head_tile, ROTSG_BRIDGE, head ? TCX_NORMAL : TCX_ON_BRIDGE) + offset;
1179  } else {
1180  seq_back[0] = SPR_TRAMWAY_BRIDGE + offset;
1181  }
1182  }
1183 
1184  /* Draw road overlay */
1185  trans_back[1] = !head && IsTransparencySet(TO_BRIDGES);
1186  if (road_rti != nullptr) {
1187  if (road_rti->UsesOverlay()) {
1188  seq_back[1] = GetCustomRoadSprite(road_rti, head_tile, ROTSG_OVERLAY, head ? TCX_NORMAL : TCX_ON_BRIDGE);
1189  if (seq_back[1] != 0) seq_back[1] += overlay_offsets[offset];
1190  }
1191  }
1192 
1193  /* Draw tram overlay */
1194  trans_back[2] = !head && IsTransparencySet(TO_BRIDGES);
1195  if (tram_rti != nullptr) {
1196  if (tram_rti->UsesOverlay()) {
1197  seq_back[2] = GetCustomRoadSprite(tram_rti, head_tile, ROTSG_OVERLAY, head ? TCX_NORMAL : TCX_ON_BRIDGE);
1198  if (seq_back[2] != 0) seq_back[2] += overlay_offsets[offset];
1199  } else if (road_rti != nullptr) {
1200  seq_back[2] = SPR_TRAMWAY_OVERLAY + overlay_offsets[offset];
1201  }
1202  }
1203 
1204  /* Road catenary takes precedence over tram */
1205  trans_back[3] = IsTransparencySet(TO_CATENARY);
1206  trans_front[0] = IsTransparencySet(TO_CATENARY);
1207  if (road_rti != nullptr && HasRoadCatenaryDrawn(road_rt)) {
1208  GetBridgeRoadCatenary(road_rti, head_tile, offset, head, seq_back[3], seq_front[0]);
1209  } else if (tram_rti != nullptr && HasRoadCatenaryDrawn(tram_rt)) {
1210  GetBridgeRoadCatenary(tram_rti, head_tile, offset, head, seq_back[3], seq_front[0]);
1211  }
1212  }
1213 
1214  static const uint size_x[6] = { 1, 16, 16, 1, 16, 1 };
1215  static const uint size_y[6] = { 16, 1, 1, 16, 1, 16 };
1216  static const uint front_bb_offset_x[6] = { 15, 0, 0, 15, 0, 15 };
1217  static const uint front_bb_offset_y[6] = { 0, 15, 15, 0, 15, 0 };
1218 
1219  /* The sprites under the vehicles are drawn as SpriteCombine. StartSpriteCombine() has already been called
1220  * The bounding boxes here are the same as for bridge front/roof */
1221  for (uint i = 0; i < lengthof(seq_back); ++i) {
1222  if (seq_back[i] != 0) {
1223  AddSortableSpriteToDraw(seq_back[i], PAL_NONE,
1224  x, y, size_x[offset], size_y[offset], 0x28, z,
1225  trans_back[i]);
1226  }
1227  }
1228 
1229  /* Start a new SpriteCombine for the front part */
1230  EndSpriteCombine();
1232 
1233  for (uint i = 0; i < lengthof(seq_front); ++i) {
1234  if (seq_front[i] != 0) {
1235  AddSortableSpriteToDraw(seq_front[i], PAL_NONE,
1236  x, y, size_x[offset] + front_bb_offset_x[offset], size_y[offset] + front_bb_offset_y[offset], 0x28, z,
1237  trans_front[i],
1238  front_bb_offset_x[offset], front_bb_offset_y[offset]);
1239  }
1240  }
1241 }
1242 
1257 {
1258  TransportType transport_type = GetTunnelBridgeTransportType(ti->tile);
1259  DiagDirection tunnelbridge_direction = GetTunnelBridgeDirection(ti->tile);
1260 
1261  if (IsTunnel(ti->tile)) {
1262  /* Front view of tunnel bounding boxes:
1263  *
1264  * 122223 <- BB_Z_SEPARATOR
1265  * 1 3
1266  * 1 3 1,3 = empty helper BB
1267  * 1 3 2 = SpriteCombine of tunnel-roof and catenary (tram & elrail)
1268  *
1269  */
1270 
1271  static const int _tunnel_BB[4][12] = {
1272  /* tunnnel-roof | Z-separator | tram-catenary
1273  * w h bb_x bb_y| x y w h |bb_x bb_y w h */
1274  { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 }, // NE
1275  { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 }, // SE
1276  { 1, 0, -15, -14, 0, 15, 16, 1, 0, 1, 16, 15 }, // SW
1277  { 0, 1, -14, -15, 15, 0, 1, 16, 1, 0, 15, 16 }, // NW
1278  };
1279  const int *BB_data = _tunnel_BB[tunnelbridge_direction];
1280 
1281  bool catenary = false;
1282 
1283  SpriteID image;
1284  SpriteID railtype_overlay = 0;
1285  if (transport_type == TRANSPORT_RAIL) {
1286  const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
1287  image = rti->base_sprites.tunnel;
1288  if (rti->UsesOverlay()) {
1289  /* Check if the railtype has custom tunnel portals. */
1290  railtype_overlay = GetCustomRailSprite(rti, ti->tile, RTSG_TUNNEL_PORTAL);
1291  if (railtype_overlay != 0) image = SPR_RAILTYPE_TUNNEL_BASE; // Draw blank grass tunnel base.
1292  }
1293  } else {
1294  image = SPR_TUNNEL_ENTRY_REAR_ROAD;
1295  }
1296 
1297  if (HasTunnelBridgeSnowOrDesert(ti->tile)) image += railtype_overlay != 0 ? 8 : 32;
1298 
1299  image += tunnelbridge_direction * 2;
1300  DrawGroundSprite(image, PAL_NONE);
1301 
1302  if (transport_type == TRANSPORT_ROAD) {
1303  RoadType road_rt = GetRoadTypeRoad(ti->tile);
1304  RoadType tram_rt = GetRoadTypeTram(ti->tile);
1305  const RoadTypeInfo *road_rti = road_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(road_rt);
1306  const RoadTypeInfo *tram_rti = tram_rt == INVALID_ROADTYPE ? nullptr : GetRoadTypeInfo(tram_rt);
1307  uint sprite_offset = DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? 1 : 0;
1308  bool draw_underlay = true;
1309 
1310  /* Road underlay takes precedence over tram */
1311  if (road_rti != nullptr) {
1312  if (road_rti->UsesOverlay()) {
1313  SpriteID ground = GetCustomRoadSprite(road_rti, ti->tile, ROTSG_TUNNEL);
1314  if (ground != 0) {
1315  DrawGroundSprite(ground + tunnelbridge_direction, PAL_NONE);
1316  draw_underlay = false;
1317  }
1318  }
1319  } else {
1320  if (tram_rti->UsesOverlay()) {
1321  SpriteID ground = GetCustomRoadSprite(tram_rti, ti->tile, ROTSG_TUNNEL);
1322  if (ground != 0) {
1323  DrawGroundSprite(ground + tunnelbridge_direction, PAL_NONE);
1324  draw_underlay = false;
1325  }
1326  }
1327  }
1328 
1329  DrawRoadOverlays(ti, PAL_NONE, road_rti, tram_rti, sprite_offset, sprite_offset, draw_underlay);
1330 
1331  /* Road catenary takes precedence over tram */
1332  SpriteID catenary_sprite_base = 0;
1333  if (road_rti != nullptr && HasRoadCatenaryDrawn(road_rt)) {
1334  catenary_sprite_base = GetCustomRoadSprite(road_rti, ti->tile, ROTSG_CATENARY_FRONT);
1335  if (catenary_sprite_base == 0) {
1336  catenary_sprite_base = SPR_TRAMWAY_TUNNEL_WIRES;
1337  } else {
1338  catenary_sprite_base += 19;
1339  }
1340  } else if (tram_rti != nullptr && HasRoadCatenaryDrawn(tram_rt)) {
1341  catenary_sprite_base = GetCustomRoadSprite(tram_rti, ti->tile, ROTSG_CATENARY_FRONT);
1342  if (catenary_sprite_base == 0) {
1343  catenary_sprite_base = SPR_TRAMWAY_TUNNEL_WIRES;
1344  } else {
1345  catenary_sprite_base += 19;
1346  }
1347  }
1348 
1349  if (catenary_sprite_base != 0) {
1350  catenary = true;
1352  AddSortableSpriteToDraw(catenary_sprite_base + tunnelbridge_direction, PAL_NONE, ti->x, ti->y, BB_data[10], BB_data[11], TILE_HEIGHT, ti->z, IsTransparencySet(TO_CATENARY), BB_data[8], BB_data[9], BB_Z_SEPARATOR);
1353  }
1354  } else {
1355  const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
1356  if (rti->UsesOverlay()) {
1357  SpriteID surface = GetCustomRailSprite(rti, ti->tile, RTSG_TUNNEL);
1358  if (surface != 0) DrawGroundSprite(surface + tunnelbridge_direction, PAL_NONE);
1359  }
1360 
1361  /* PBS debugging, draw reserved tracks darker */
1362  if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasTunnelBridgeReservation(ti->tile)) {
1363  if (rti->UsesOverlay()) {
1364  SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
1365  DrawGroundSprite(overlay + RTO_X + DiagDirToAxis(tunnelbridge_direction), PALETTE_CRASH);
1366  } else {
1367  DrawGroundSprite(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH);
1368  }
1369  }
1370 
1372  /* Maybe draw pylons on the entry side */
1373  DrawRailCatenary(ti);
1374 
1375  catenary = true;
1377  /* Draw wire above the ramp */
1379  }
1380  }
1381 
1382  if (railtype_overlay != 0 && !catenary) StartSpriteCombine();
1383 
1384  AddSortableSpriteToDraw(image + 1, PAL_NONE, ti->x + TILE_SIZE - 1, ti->y + TILE_SIZE - 1, BB_data[0], BB_data[1], TILE_HEIGHT, ti->z, false, BB_data[2], BB_data[3], BB_Z_SEPARATOR);
1385  /* Draw railtype tunnel portal overlay if defined. */
1386  if (railtype_overlay != 0) AddSortableSpriteToDraw(railtype_overlay + tunnelbridge_direction, PAL_NONE, ti->x + TILE_SIZE - 1, ti->y + TILE_SIZE - 1, BB_data[0], BB_data[1], TILE_HEIGHT, ti->z, false, BB_data[2], BB_data[3], BB_Z_SEPARATOR);
1387 
1388  if (catenary || railtype_overlay != 0) EndSpriteCombine();
1389 
1390  /* Add helper BB for sprite sorting that separates the tunnel from things beside of it. */
1391  AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x, ti->y, BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
1392  AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, ti->x + BB_data[4], ti->y + BB_data[5], BB_data[6], BB_data[7], TILE_HEIGHT, ti->z);
1393 
1394  DrawBridgeMiddle(ti);
1395  } else { // IsBridge(ti->tile)
1396  const PalSpriteID *psid;
1397  int base_offset;
1398  bool ice = HasTunnelBridgeSnowOrDesert(ti->tile);
1399 
1400  if (transport_type == TRANSPORT_RAIL) {
1401  base_offset = GetRailTypeInfo(GetRailType(ti->tile))->bridge_offset;
1402  assert(base_offset != 8); // This one is used for roads
1403  } else {
1404  base_offset = 8;
1405  }
1406 
1407  /* as the lower 3 bits are used for other stuff, make sure they are clear */
1408  assert( (base_offset & 0x07) == 0x00);
1409 
1410  DrawFoundation(ti, GetBridgeFoundation(ti->tileh, DiagDirToAxis(tunnelbridge_direction)));
1411 
1412  /* HACK Wizardry to convert the bridge ramp direction into a sprite offset */
1413  base_offset += (6 - tunnelbridge_direction) % 4;
1414 
1415  /* Table number BRIDGE_PIECE_HEAD always refers to the bridge heads for any bridge type */
1416  if (transport_type != TRANSPORT_WATER) {
1417  if (ti->tileh == SLOPE_FLAT) base_offset += 4; // sloped bridge head
1418  psid = &GetBridgeSpriteTable(GetBridgeType(ti->tile), BRIDGE_PIECE_HEAD)[base_offset];
1419  } else {
1420  psid = _aqueduct_sprites + base_offset;
1421  }
1422 
1423  if (!ice) {
1424  TileIndex next = ti->tile + TileOffsByDiagDir(tunnelbridge_direction);
1425  if (ti->tileh != SLOPE_FLAT && ti->z == 0 && HasTileWaterClass(next) && GetWaterClass(next) == WATER_CLASS_SEA) {
1426  DrawShoreTile(ti->tileh);
1427  } else {
1428  DrawClearLandTile(ti, 3);
1429  }
1430  } else {
1431  DrawGroundSprite(SPR_FLAT_SNOW_DESERT_TILE + SlopeToSpriteOffset(ti->tileh), PAL_NONE);
1432  }
1433 
1434  /* draw ramp */
1435 
1436  /* Draw Trambits and PBS Reservation as SpriteCombine */
1437  if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
1438 
1439  /* HACK set the height of the BB of a sloped ramp to 1 so a vehicle on
1440  * it doesn't disappear behind it
1441  */
1442  /* Bridge heads are drawn solid no matter how invisibility/transparency is set */
1443  AddSortableSpriteToDraw(psid->sprite, psid->pal, ti->x, ti->y, 16, 16, ti->tileh == SLOPE_FLAT ? 0 : 8, ti->z);
1444 
1445  if (transport_type == TRANSPORT_ROAD) {
1446  uint offset = tunnelbridge_direction;
1447  int z = ti->z;
1448  if (ti->tileh != SLOPE_FLAT) {
1449  offset = (offset + 1) & 1;
1450  z += TILE_HEIGHT;
1451  } else {
1452  offset += 2;
1453  }
1454 
1455  /* DrawBridgeRoadBits() calls EndSpriteCombine() and StartSpriteCombine() */
1456  DrawBridgeRoadBits(ti->tile, ti->x, ti->y, z, offset, true);
1457 
1458  EndSpriteCombine();
1459  } else if (transport_type == TRANSPORT_RAIL) {
1460  const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
1461  if (rti->UsesOverlay()) {
1462  SpriteID surface = GetCustomRailSprite(rti, ti->tile, RTSG_BRIDGE);
1463  if (surface != 0) {
1464  if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
1465  AddSortableSpriteToDraw(surface + ((DiagDirToAxis(tunnelbridge_direction) == AXIS_X) ? RTBO_X : RTBO_Y), PAL_NONE, ti->x, ti->y, 16, 16, 0, ti->z + 8);
1466  } else {
1467  AddSortableSpriteToDraw(surface + RTBO_SLOPE + tunnelbridge_direction, PAL_NONE, ti->x, ti->y, 16, 16, 8, ti->z);
1468  }
1469  }
1470  /* Don't fallback to non-overlay sprite -- the spec states that
1471  * if an overlay is present then the bridge surface must be
1472  * present. */
1473  }
1474 
1475  /* PBS debugging, draw reserved tracks darker */
1476  if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasTunnelBridgeReservation(ti->tile)) {
1477  if (rti->UsesOverlay()) {
1478  SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
1479  if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
1480  AddSortableSpriteToDraw(overlay + RTO_X + DiagDirToAxis(tunnelbridge_direction), PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, ti->z + 8);
1481  } else {
1482  AddSortableSpriteToDraw(overlay + RTO_SLOPE_NE + tunnelbridge_direction, PALETTE_CRASH, ti->x, ti->y, 16, 16, 8, ti->z);
1483  }
1484  } else {
1485  if (HasBridgeFlatRamp(ti->tileh, DiagDirToAxis(tunnelbridge_direction))) {
1486  AddSortableSpriteToDraw(DiagDirToAxis(tunnelbridge_direction) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, ti->z + 8);
1487  } else {
1488  AddSortableSpriteToDraw(rti->base_sprites.single_sloped + tunnelbridge_direction, PALETTE_CRASH, ti->x, ti->y, 16, 16, 8, ti->z);
1489  }
1490  }
1491  }
1492 
1493  EndSpriteCombine();
1495  DrawRailCatenary(ti);
1496  }
1497  }
1498 
1499  DrawBridgeMiddle(ti);
1500  }
1501 }
1502 
1503 
1522 static BridgePieces CalcBridgePiece(uint north, uint south)
1523 {
1524  if (north == 1) {
1525  return BRIDGE_PIECE_NORTH;
1526  } else if (south == 1) {
1527  return BRIDGE_PIECE_SOUTH;
1528  } else if (north < south) {
1529  return north & 1 ? BRIDGE_PIECE_INNER_SOUTH : BRIDGE_PIECE_INNER_NORTH;
1530  } else if (north > south) {
1531  return south & 1 ? BRIDGE_PIECE_INNER_NORTH : BRIDGE_PIECE_INNER_SOUTH;
1532  } else {
1533  return north & 1 ? BRIDGE_PIECE_MIDDLE_EVEN : BRIDGE_PIECE_MIDDLE_ODD;
1534  }
1535 }
1536 
1542 {
1543  /* Sectional view of bridge bounding boxes:
1544  *
1545  * 1 2 1,2 = SpriteCombine of Bridge front/(back&floor) and RoadCatenary
1546  * 1 2 3 = empty helper BB
1547  * 1 7 2 4,5 = pillars under higher bridges
1548  * 1 6 88888 6 2 6 = elrail-pylons
1549  * 1 6 88888 6 2 7 = elrail-wire
1550  * 1 6 88888 6 2 <- TILE_HEIGHT 8 = rail-vehicle on bridge
1551  * 3333333333333 <- BB_Z_SEPARATOR
1552  * <- unused
1553  * 4 5 <- BB_HEIGHT_UNDER_BRIDGE
1554  * 4 5
1555  * 4 5
1556  *
1557  */
1558 
1559  if (!IsBridgeAbove(ti->tile)) return;
1560 
1561  TileIndex rampnorth = GetNorthernBridgeEnd(ti->tile);
1562  TileIndex rampsouth = GetSouthernBridgeEnd(ti->tile);
1563  TransportType transport_type = GetTunnelBridgeTransportType(rampsouth);
1564 
1565  Axis axis = GetBridgeAxis(ti->tile);
1566  BridgePieces piece = CalcBridgePiece(
1567  GetTunnelBridgeLength(ti->tile, rampnorth) + 1,
1568  GetTunnelBridgeLength(ti->tile, rampsouth) + 1
1569  );
1570 
1571  const PalSpriteID *psid;
1572  bool drawfarpillar;
1573  if (transport_type != TRANSPORT_WATER) {
1574  BridgeType type = GetBridgeType(rampsouth);
1575  drawfarpillar = !HasBit(GetBridgeSpec(type)->flags, 0);
1576 
1577  uint base_offset;
1578  if (transport_type == TRANSPORT_RAIL) {
1579  base_offset = GetRailTypeInfo(GetRailType(rampsouth))->bridge_offset;
1580  } else {
1581  base_offset = 8;
1582  }
1583 
1584  psid = base_offset + GetBridgeSpriteTable(type, piece);
1585  } else {
1586  drawfarpillar = true;
1587  psid = _aqueduct_sprites;
1588  }
1589 
1590  if (axis != AXIS_X) psid += 4;
1591 
1592  int x = ti->x;
1593  int y = ti->y;
1594  uint bridge_z = GetBridgePixelHeight(rampsouth);
1595  int z = bridge_z - BRIDGE_Z_START;
1596 
1597  /* Add a bounding box that separates the bridge from things below it. */
1598  AddSortableSpriteToDraw(SPR_EMPTY_BOUNDING_BOX, PAL_NONE, x, y, 16, 16, 1, bridge_z - TILE_HEIGHT + BB_Z_SEPARATOR);
1599 
1600  /* Draw Trambits as SpriteCombine */
1601  if (transport_type == TRANSPORT_ROAD || transport_type == TRANSPORT_RAIL) StartSpriteCombine();
1602 
1603  /* Draw floor and far part of bridge*/
1604  if (!IsInvisibilitySet(TO_BRIDGES)) {
1605  if (axis == AXIS_X) {
1606  AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 1, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
1607  } else {
1608  AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 1, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 0, BRIDGE_Z_START);
1609  }
1610  }
1611 
1612  psid++;
1613 
1614  if (transport_type == TRANSPORT_ROAD) {
1615  /* DrawBridgeRoadBits() calls EndSpriteCombine() and StartSpriteCombine() */
1616  DrawBridgeRoadBits(rampsouth, x, y, bridge_z, axis ^ 1, false);
1617  } else if (transport_type == TRANSPORT_RAIL) {
1618  const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(rampsouth));
1619  if (rti->UsesOverlay() && !IsInvisibilitySet(TO_BRIDGES)) {
1620  SpriteID surface = GetCustomRailSprite(rti, rampsouth, RTSG_BRIDGE, TCX_ON_BRIDGE);
1621  if (surface != 0) {
1622  AddSortableSpriteToDraw(surface + axis, PAL_NONE, x, y, 16, 16, 0, bridge_z, IsTransparencySet(TO_BRIDGES));
1623  }
1624  }
1625 
1626  if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && !IsInvisibilitySet(TO_BRIDGES) && HasTunnelBridgeReservation(rampnorth)) {
1627  if (rti->UsesOverlay()) {
1628  SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
1629  AddSortableSpriteToDraw(overlay + RTO_X + axis, PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, bridge_z, IsTransparencySet(TO_BRIDGES));
1630  } else {
1631  AddSortableSpriteToDraw(axis == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH, ti->x, ti->y, 16, 16, 0, bridge_z, IsTransparencySet(TO_BRIDGES));
1632  }
1633  }
1634 
1635  EndSpriteCombine();
1636 
1637  if (HasRailCatenaryDrawn(GetRailType(rampsouth))) {
1639  }
1640  }
1641 
1642  /* draw roof, the component of the bridge which is logically between the vehicle and the camera */
1643  if (!IsInvisibilitySet(TO_BRIDGES)) {
1644  if (axis == AXIS_X) {
1645  y += 12;
1646  if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 16, 4, 0x28, z, IsTransparencySet(TO_BRIDGES), 0, 3, BRIDGE_Z_START);
1647  } else {
1648  x += 12;
1649  if (psid->sprite & SPRITE_MASK) AddSortableSpriteToDraw(psid->sprite, psid->pal, x, y, 4, 16, 0x28, z, IsTransparencySet(TO_BRIDGES), 3, 0, BRIDGE_Z_START);
1650  }
1651  }
1652 
1653  /* Draw TramFront as SpriteCombine */
1654  if (transport_type == TRANSPORT_ROAD) EndSpriteCombine();
1655 
1656  /* Do not draw anything more if bridges are invisible */
1657  if (IsInvisibilitySet(TO_BRIDGES)) return;
1658 
1659  psid++;
1660  DrawBridgePillars(psid, ti, axis, drawfarpillar, x, y, z);
1661 }
1662 
1663 
1664 static int GetSlopePixelZ_TunnelBridge(TileIndex tile, uint x, uint y, bool ground_vehicle)
1665 {
1666  auto [tileh, z] = GetTilePixelSlope(tile);
1667 
1668  x &= 0xF;
1669  y &= 0xF;
1670 
1671  if (IsTunnel(tile)) {
1672  /* In the tunnel entrance? */
1673  if (ground_vehicle) return z;
1674  } else { // IsBridge(tile)
1677 
1678  /* On the bridge ramp? */
1679  if (ground_vehicle) {
1680  if (tileh != SLOPE_FLAT) return z + TILE_HEIGHT;
1681 
1682  switch (dir) {
1683  default: NOT_REACHED();
1684  case DIAGDIR_NE: tileh = SLOPE_NE; break;
1685  case DIAGDIR_SE: tileh = SLOPE_SE; break;
1686  case DIAGDIR_SW: tileh = SLOPE_SW; break;
1687  case DIAGDIR_NW: tileh = SLOPE_NW; break;
1688  }
1689  }
1690  }
1691 
1692  return z + GetPartialPixelZ(x, y, tileh);
1693 }
1694 
1695 static Foundation GetFoundation_TunnelBridge(TileIndex tile, Slope tileh)
1696 {
1698 }
1699 
1700 static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
1701 {
1703 
1704  if (IsTunnel(tile)) {
1705  td->str = (tt == TRANSPORT_RAIL) ? STR_LAI_TUNNEL_DESCRIPTION_RAILROAD : STR_LAI_TUNNEL_DESCRIPTION_ROAD;
1706  } else { // IsBridge(tile)
1707  td->str = (tt == TRANSPORT_WATER) ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : GetBridgeSpec(GetBridgeType(tile))->transport_name[tt];
1708  }
1709  td->owner[0] = GetTileOwner(tile);
1710 
1711  Owner road_owner = INVALID_OWNER;
1712  Owner tram_owner = INVALID_OWNER;
1713  RoadType road_rt = GetRoadTypeRoad(tile);
1714  RoadType tram_rt = GetRoadTypeTram(tile);
1715  if (road_rt != INVALID_ROADTYPE) {
1716  const RoadTypeInfo *rti = GetRoadTypeInfo(road_rt);
1717  td->roadtype = rti->strings.name;
1718  td->road_speed = rti->max_speed / 2;
1719  road_owner = GetRoadOwner(tile, RTT_ROAD);
1720  }
1721  if (tram_rt != INVALID_ROADTYPE) {
1722  const RoadTypeInfo *rti = GetRoadTypeInfo(tram_rt);
1723  td->tramtype = rti->strings.name;
1724  td->tram_speed = rti->max_speed / 2;
1725  tram_owner = GetRoadOwner(tile, RTT_TRAM);
1726  }
1727 
1728  /* Is there a mix of owners? */
1729  if ((tram_owner != INVALID_OWNER && tram_owner != td->owner[0]) ||
1730  (road_owner != INVALID_OWNER && road_owner != td->owner[0])) {
1731  uint i = 1;
1732  if (road_owner != INVALID_OWNER) {
1733  td->owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
1734  td->owner[i] = road_owner;
1735  i++;
1736  }
1737  if (tram_owner != INVALID_OWNER) {
1738  td->owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
1739  td->owner[i] = tram_owner;
1740  }
1741  }
1742 
1743  if (tt == TRANSPORT_RAIL) {
1744  const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
1745  td->rail_speed = rti->max_speed;
1746  td->railtype = rti->strings.name;
1747 
1748  if (!IsTunnel(tile)) {
1749  uint16_t spd = GetBridgeSpec(GetBridgeType(tile))->speed;
1750  /* rail speed special-cases 0 as unlimited, hides display of limit etc. */
1751  if (spd == UINT16_MAX) spd = 0;
1752  if (td->rail_speed == 0 || spd < td->rail_speed) {
1753  td->rail_speed = spd;
1754  }
1755  }
1756  } else if (tt == TRANSPORT_ROAD && !IsTunnel(tile)) {
1757  uint16_t spd = GetBridgeSpec(GetBridgeType(tile))->speed;
1758  /* road speed special-cases 0 as unlimited, hides display of limit etc. */
1759  if (spd == UINT16_MAX) spd = 0;
1760  if (road_rt != INVALID_ROADTYPE && (td->road_speed == 0 || spd < td->road_speed)) td->road_speed = spd;
1761  if (tram_rt != INVALID_ROADTYPE && (td->tram_speed == 0 || spd < td->tram_speed)) td->tram_speed = spd;
1762  }
1763 }
1764 
1765 
1766 static void TileLoop_TunnelBridge(TileIndex tile)
1767 {
1768  bool snow_or_desert = HasTunnelBridgeSnowOrDesert(tile);
1770  case LT_ARCTIC: {
1771  /* As long as we do not have a snow density, we want to use the density
1772  * from the entry edge. For tunnels this is the lowest point for bridges the highest point.
1773  * (Independent of foundations) */
1774  int z = IsBridge(tile) ? GetTileMaxZ(tile) : GetTileZ(tile);
1775  if (snow_or_desert != (z > GetSnowLine())) {
1776  SetTunnelBridgeSnowOrDesert(tile, !snow_or_desert);
1777  MarkTileDirtyByTile(tile);
1778  }
1779  break;
1780  }
1781 
1782  case LT_TROPIC:
1783  if (GetTropicZone(tile) == TROPICZONE_DESERT && !snow_or_desert) {
1784  SetTunnelBridgeSnowOrDesert(tile, true);
1785  MarkTileDirtyByTile(tile);
1786  }
1787  break;
1788 
1789  default:
1790  break;
1791  }
1792 }
1793 
1794 static TrackStatus GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
1795 {
1796  TransportType transport_type = GetTunnelBridgeTransportType(tile);
1797  if (transport_type != mode || (transport_type == TRANSPORT_ROAD && !HasTileRoadType(tile, (RoadTramType)sub_mode))) return 0;
1798 
1800  if (side != INVALID_DIAGDIR && side != ReverseDiagDir(dir)) return 0;
1802 }
1803 
1804 static void ChangeTileOwner_TunnelBridge(TileIndex tile, Owner old_owner, Owner new_owner)
1805 {
1806  TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
1807  /* Set number of pieces to zero if it's the southern tile as we
1808  * don't want to update the infrastructure counts twice. */
1809  uint num_pieces = tile < other_end ? (GetTunnelBridgeLength(tile, other_end) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR : 0;
1810 
1811  for (RoadTramType rtt : _roadtramtypes) {
1812  /* Update all roadtypes, no matter if they are present */
1813  if (GetRoadOwner(tile, rtt) == old_owner) {
1814  RoadType rt = GetRoadType(tile, rtt);
1815  if (rt != INVALID_ROADTYPE) {
1816  /* Update company infrastructure counts. A full diagonal road tile has two road bits.
1817  * No need to dirty windows here, we'll redraw the whole screen anyway. */
1818  Company::Get(old_owner)->infrastructure.road[rt] -= num_pieces * 2;
1819  if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.road[rt] += num_pieces * 2;
1820  }
1821 
1822  SetRoadOwner(tile, rtt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
1823  }
1824  }
1825 
1826  if (!IsTileOwner(tile, old_owner)) return;
1827 
1828  /* Update company infrastructure counts for rail and water as well.
1829  * No need to dirty windows here, we'll redraw the whole screen anyway. */
1831  Company *old = Company::Get(old_owner);
1832  if (tt == TRANSPORT_RAIL) {
1833  old->infrastructure.rail[GetRailType(tile)] -= num_pieces;
1834  if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.rail[GetRailType(tile)] += num_pieces;
1835  } else if (tt == TRANSPORT_WATER) {
1836  old->infrastructure.water -= num_pieces;
1837  if (new_owner != INVALID_OWNER) Company::Get(new_owner)->infrastructure.water += num_pieces;
1838  }
1839 
1840  if (new_owner != INVALID_OWNER) {
1841  SetTileOwner(tile, new_owner);
1842  } else {
1843  if (tt == TRANSPORT_RAIL) {
1844  /* Since all of our vehicles have been removed, it is safe to remove the rail
1845  * bridge / tunnel. */
1846  [[maybe_unused]] CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(DC_EXEC | DC_BANKRUPT, tile);
1847  assert(ret.Succeeded());
1848  } else {
1849  /* In any other case, we can safely reassign the ownership to OWNER_NONE. */
1850  SetTileOwner(tile, OWNER_NONE);
1851  }
1852  }
1853 }
1854 
1867 template <typename T>
1868 static void PrepareToEnterBridge(T *gv)
1869 {
1870  if (HasBit(gv->gv_flags, GVF_GOINGUP_BIT)) {
1871  gv->z_pos++;
1872  ClrBit(gv->gv_flags, GVF_GOINGUP_BIT);
1873  } else {
1874  ClrBit(gv->gv_flags, GVF_GOINGDOWN_BIT);
1875  }
1876 }
1877 
1883 static const uint8_t TUNNEL_SOUND_FRAME = 1;
1884 
1893 extern const uint8_t _tunnel_visibility_frame[DIAGDIR_END] = {12, 8, 8, 12};
1894 
1895 static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
1896 {
1897  int z = GetSlopePixelZ(x, y, true) - v->z_pos;
1898 
1899  if (abs(z) > 2) return VETSB_CANNOT_ENTER;
1900  /* Direction into the wormhole */
1901  const DiagDirection dir = GetTunnelBridgeDirection(tile);
1902  /* Direction of the vehicle */
1903  const DiagDirection vdir = DirToDiagDir(v->direction);
1904  /* New position of the vehicle on the tile */
1905  uint8_t pos = (DiagDirToAxis(vdir) == AXIS_X ? x : y) & TILE_UNIT_MASK;
1906  /* Number of units moved by the vehicle since entering the tile */
1907  uint8_t frame = (vdir == DIAGDIR_NE || vdir == DIAGDIR_NW) ? TILE_SIZE - 1 - pos : pos;
1908 
1909  if (IsTunnel(tile)) {
1910  if (v->type == VEH_TRAIN) {
1911  Train *t = Train::From(v);
1912 
1913  if (t->track != TRACK_BIT_WORMHOLE && dir == vdir) {
1914  if (t->IsFrontEngine() && frame == TUNNEL_SOUND_FRAME) {
1915  if (!PlayVehicleSound(t, VSE_TUNNEL) && RailVehInfo(t->engine_type)->engclass == 0) {
1916  SndPlayVehicleFx(SND_05_TRAIN_THROUGH_TUNNEL, v);
1917  }
1918  return VETSB_CONTINUE;
1919  }
1920  if (frame == _tunnel_visibility_frame[dir]) {
1921  t->tile = tile;
1922  t->track = TRACK_BIT_WORMHOLE;
1923  t->vehstatus |= VS_HIDDEN;
1924  return VETSB_ENTERED_WORMHOLE;
1925  }
1926  }
1927 
1928  if (dir == ReverseDiagDir(vdir) && frame == TILE_SIZE - _tunnel_visibility_frame[dir] && z == 0) {
1929  /* We're at the tunnel exit ?? */
1930  t->tile = tile;
1931  t->track = DiagDirToDiagTrackBits(vdir);
1932  assert(t->track);
1933  t->vehstatus &= ~VS_HIDDEN;
1934  return VETSB_ENTERED_WORMHOLE;
1935  }
1936  } else if (v->type == VEH_ROAD) {
1937  RoadVehicle *rv = RoadVehicle::From(v);
1938 
1939  /* Enter tunnel? */
1940  if (rv->state != RVSB_WORMHOLE && dir == vdir) {
1941  if (frame == _tunnel_visibility_frame[dir]) {
1942  /* Frame should be equal to the next frame number in the RV's movement */
1943  assert(frame == rv->frame + 1);
1944  rv->tile = tile;
1945  rv->state = RVSB_WORMHOLE;
1946  rv->vehstatus |= VS_HIDDEN;
1947  return VETSB_ENTERED_WORMHOLE;
1948  } else {
1949  return VETSB_CONTINUE;
1950  }
1951  }
1952 
1953  /* We're at the tunnel exit ?? */
1954  if (dir == ReverseDiagDir(vdir) && frame == TILE_SIZE - _tunnel_visibility_frame[dir] && z == 0) {
1955  rv->tile = tile;
1956  rv->state = DiagDirToDiagTrackdir(vdir);
1957  rv->frame = frame;
1958  rv->vehstatus &= ~VS_HIDDEN;
1959  return VETSB_ENTERED_WORMHOLE;
1960  }
1961  }
1962  } else { // IsBridge(tile)
1963  if (v->type != VEH_SHIP) {
1964  /* modify speed of vehicle */
1965  uint16_t spd = GetBridgeSpec(GetBridgeType(tile))->speed;
1966 
1967  if (v->type == VEH_ROAD) spd *= 2;
1968  Vehicle *first = v->First();
1969  first->cur_speed = std::min(first->cur_speed, spd);
1970  }
1971 
1972  if (vdir == dir) {
1973  /* Vehicle enters bridge at the last frame inside this tile. */
1974  if (frame != TILE_SIZE - 1) return VETSB_CONTINUE;
1975  switch (v->type) {
1976  case VEH_TRAIN: {
1977  Train *t = Train::From(v);
1978  t->track = TRACK_BIT_WORMHOLE;
1980  break;
1981  }
1982 
1983  case VEH_ROAD: {
1984  RoadVehicle *rv = RoadVehicle::From(v);
1985  rv->state = RVSB_WORMHOLE;
1987  break;
1988  }
1989 
1990  case VEH_SHIP:
1992  break;
1993 
1994  default: NOT_REACHED();
1995  }
1996  return VETSB_ENTERED_WORMHOLE;
1997  } else if (vdir == ReverseDiagDir(dir)) {
1998  v->tile = tile;
1999  switch (v->type) {
2000  case VEH_TRAIN: {
2001  Train *t = Train::From(v);
2002  if (t->track == TRACK_BIT_WORMHOLE) {
2003  t->track = DiagDirToDiagTrackBits(vdir);
2004  return VETSB_ENTERED_WORMHOLE;
2005  }
2006  break;
2007  }
2008 
2009  case VEH_ROAD: {
2010  RoadVehicle *rv = RoadVehicle::From(v);
2011  if (rv->state == RVSB_WORMHOLE) {
2012  rv->state = DiagDirToDiagTrackdir(vdir);
2013  rv->frame = 0;
2014  return VETSB_ENTERED_WORMHOLE;
2015  }
2016  break;
2017  }
2018 
2019  case VEH_SHIP: {
2020  Ship *ship = Ship::From(v);
2021  if (ship->state == TRACK_BIT_WORMHOLE) {
2022  ship->state = DiagDirToDiagTrackBits(vdir);
2023  return VETSB_ENTERED_WORMHOLE;
2024  }
2025  break;
2026  }
2027 
2028  default: NOT_REACHED();
2029  }
2030  }
2031  }
2032  return VETSB_CONTINUE;
2033 }
2034 
2035 static CommandCost TerraformTile_TunnelBridge(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
2036 {
2038  DiagDirection direction = GetTunnelBridgeDirection(tile);
2039  Axis axis = DiagDirToAxis(direction);
2040  CommandCost res;
2041  auto [tileh_old, z_old] = GetTileSlopeZ(tile);
2042 
2043  /* Check if new slope is valid for bridges in general (so we can safely call GetBridgeFoundation()) */
2044  if ((direction == DIAGDIR_NW) || (direction == DIAGDIR_NE)) {
2045  CheckBridgeSlope(BRIDGE_PIECE_SOUTH, axis, tileh_old, z_old);
2046  res = CheckBridgeSlope(BRIDGE_PIECE_SOUTH, axis, tileh_new, z_new);
2047  } else {
2048  CheckBridgeSlope(BRIDGE_PIECE_NORTH, axis, tileh_old, z_old);
2049  res = CheckBridgeSlope(BRIDGE_PIECE_NORTH, axis, tileh_new, z_new);
2050  }
2051 
2052  /* Surface slope is valid and remains unchanged? */
2053  if (res.Succeeded() && (z_old == z_new) && (tileh_old == tileh_new)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
2054  }
2055 
2056  return Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile);
2057 }
2058 
2059 extern const TileTypeProcs _tile_type_tunnelbridge_procs = {
2060  DrawTile_TunnelBridge, // draw_tile_proc
2061  GetSlopePixelZ_TunnelBridge, // get_slope_z_proc
2062  ClearTile_TunnelBridge, // clear_tile_proc
2063  nullptr, // add_accepted_cargo_proc
2064  GetTileDesc_TunnelBridge, // get_tile_desc_proc
2065  GetTileTrackStatus_TunnelBridge, // get_tile_track_status_proc
2066  nullptr, // click_tile_proc
2067  nullptr, // animate_tile_proc
2068  TileLoop_TunnelBridge, // tile_loop_proc
2069  ChangeTileOwner_TunnelBridge, // change_tile_owner_proc
2070  nullptr, // add_produced_cargo_proc
2071  VehicleEnter_TunnelBridge, // vehicle_enter_tile_proc
2072  GetFoundation_TunnelBridge, // get_foundation_proc
2073  TerraformTile_TunnelBridge, // terraform_tile_proc
2074 };
INVALID_RAILTYPE
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition: rail_type.h:34
BRIDGE_Z_START
static const int BRIDGE_Z_START
Z position of the bridge sprites relative to bridge height (downwards)
Definition: tunnelbridge_cmd.cpp:56
RoadVehicle
Buses, trucks and trams belong to this class.
Definition: roadveh.h:106
DoClearTunnel
static CommandCost DoClearTunnel(TileIndex tile, DoCommandFlag flags)
Remove a tunnel from the game, update town rating, etc.
Definition: tunnelbridge_cmd.cpp:845
SLOPE_SE
@ SLOPE_SE
south and east corner are raised
Definition: slope_type.h:57
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:437
TileInfo::z
int z
Height.
Definition: tile_cmd.h:48
MP_CLEAR
@ MP_CLEAR
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition: tile_type.h:48
Vehicle::IsFrontEngine
debug_inline bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition: vehicle_base.h:945
SPRITE_MASK
@ SPRITE_MASK
The mask to for the main sprite.
Definition: sprites.h:1560
CompanyProperties::is_ai
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Definition: company_base.h:112
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
tunnelbridge.h
VETSB_CANNOT_ENTER
@ VETSB_CANNOT_ENTER
The vehicle cannot enter the tile.
Definition: tile_cmd.h:38
TROPICZONE_DESERT
@ TROPICZONE_DESERT
Tile is desert.
Definition: tile_type.h:78
RoadTypeInfo
Definition: road.h:78
DIAGDIR_NE
@ DIAGDIR_NE
Northeast, upper right on your monitor.
Definition: direction_type.h:75
TO_CATENARY
@ TO_CATENARY
catenary
Definition: transparency.h:30
sound_func.h
Cheats::crossing_tunnels
Cheat crossing_tunnels
allow tunnels that cross each other
Definition: cheat_type.h:30
Cheats::magic_bulldozer
Cheat magic_bulldozer
dynamite industries, objects
Definition: cheat_type.h:27
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:339
GetOtherBridgeEnd
TileIndex GetOtherBridgeEnd(TileIndex tile)
Starting at one bridge end finds the other bridge end.
Definition: bridge_map.cpp:59
water.h
GetTileMaxZ
int GetTileMaxZ(TileIndex t)
Get top height of the tile inside the map.
Definition: tile_map.cpp:136
ResetBridges
void ResetBridges()
Reset the data been eventually changed by the grf loaded.
Definition: tunnelbridge_cmd.cpp:86
train.h
ROADSIDE_TREES
@ ROADSIDE_TREES
Road with trees on paved sidewalks.
Definition: road_map.h:483
BB_Z_SEPARATOR
static const uint BB_Z_SEPARATOR
Separates the bridge/tunnel from the things under/above it.
Definition: viewport_type.h:89
ValParamRailType
bool ValParamRailType(const RailType rail)
Validate functions for rail building.
Definition: rail.cpp:206
command_func.h
VSE_TUNNEL
@ VSE_TUNNEL
Train entering a tunnel.
Definition: newgrf_sound.h:20
ObjectSpec
Allow incrementing of ObjectClassID variables.
Definition: newgrf_object.h:60
YapfNotifyTrackLayoutChange
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
Use this function to notify YAPF that track layout (or signal configuration) has change.
Definition: yapf_rail.cpp:665
GetTunnelBridgeLength
uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
Calculates the length of a tunnel or a bridge (without end tiles)
Definition: tunnelbridge.h:25
Swap
constexpr void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:283
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:350
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
FlatteningFoundation
Foundation FlatteningFoundation(Slope s)
Returns the foundation needed to flatten a slope.
Definition: slope_func.h:369
GetRailTypeInfo
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:307
ClosestTownFromTile
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
Definition: town_cmd.cpp:3864
TileInfo
Tile information, used while rendering the tile.
Definition: tile_cmd.h:43
RATING_TUNNEL_BRIDGE_MINIMUM
@ RATING_TUNNEL_BRIDGE_MINIMUM
minimum rating after removing tunnel or bridge
Definition: town_type.h:58
GetWaterClass
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition: water_map.h:115
TileDesc::railtype
StringID railtype
Type of rail on the tile.
Definition: tile_cmd.h:64
Map::MaxX
static debug_inline uint MaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
Definition: map_func.h:297
terraform_cmd.h
RailTypeInfo::tunnel
SpriteID tunnel
tunnel sprites base
Definition: rail.h:145
company_base.h
HasBridgeFlatRamp
bool HasBridgeFlatRamp(Slope tileh, Axis axis)
Determines if the track on a bridge ramp is flat or goes up/down.
Definition: tunnelbridge_cmd.cpp:145
tunnelbridge_map.h
timer_game_calendar.h
RailClearCost
Money RailClearCost(RailType railtype)
Returns the 'cost' of clearing the specified railtype.
Definition: rail.h:386
TileDesc::owner
Owner owner[4]
Name of the owner(s)
Definition: tile_cmd.h:55
SLOPE_NE
@ SLOPE_NE
north and east corner are raised
Definition: slope_type.h:58
WATER_CLASS_SEA
@ WATER_CLASS_SEA
Sea.
Definition: water_map.h:48
TRANSPORT_WATER
@ TRANSPORT_WATER
Transport over water.
Definition: transport_type.h:29
company_gui.h
IsPlainRail
static debug_inline bool IsPlainRail(Tile t)
Returns whether this is plain rails, with or without signals.
Definition: rail_map.h:49
INVALID_OWNER
@ INVALID_OWNER
An invalid owner.
Definition: company_type.h:29
RailTypeInfo::bridge_offset
SpriteID bridge_offset
Bridge offset.
Definition: rail.h:196
elrail_func.h
SND_05_TRAIN_THROUGH_TUNNEL
@ SND_05_TRAIN_THROUGH_TUNNEL
3 == 0x03 Train enters tunnel: steam engine
Definition: sound_type.h:42
GetRoadside
Roadside GetRoadside(Tile tile)
Get the decorations of a road.
Definition: road_map.h:493
Price
Price
Enumeration of all base prices for use with Prices.
Definition: economy_type.h:89
InclinedFoundation
Foundation InclinedFoundation(Axis axis)
Returns the along a specific axis inclined foundation.
Definition: slope_func.h:380
DrawPillar
static void DrawPillar(const PalSpriteID *psid, int x, int y, int z, int w, int h, const SubSprite *subsprite)
Draw a single pillar sprite.
Definition: tunnelbridge_cmd.cpp:1037
SLOPE_FLAT
@ SLOPE_FLAT
a flat tile
Definition: slope_type.h:49
TileDesc::road_speed
uint16_t road_speed
Speed limit of road (bridges and track)
Definition: tile_cmd.h:67
SetTunnelBridgeReservation
void SetTunnelBridgeReservation(Tile t, bool b)
Set the reservation state of the rail tunnel/bridge.
Definition: tunnelbridge_map.h:104
OBJECT_FLAG_ALLOW_UNDER_BRIDGE
@ OBJECT_FLAG_ALLOW_UNDER_BRIDGE
Object can built under a bridge.
Definition: newgrf_object.h:37
AutoslopeEnabled
bool AutoslopeEnabled()
Tests if autoslope is enabled for _current_company.
Definition: autoslope.h:44
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
IsTransparencySet
bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:48
RailType
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:27
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
BridgeSpec::sprite_table
PalSpriteID ** sprite_table
table of sprites for drawing the bridge
Definition: bridge.h:52
DiagDirToAxis
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
Definition: direction_func.h:214
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
PalSpriteID::sprite
SpriteID sprite
The 'real' sprite.
Definition: gfx_type.h:24
GetBridgeHeight
int GetBridgeHeight(TileIndex t)
Get the height ('z') of a bridge.
Definition: bridge_map.cpp:70
HasTunnelBridgeReservation
bool HasTunnelBridgeReservation(Tile t)
Get the reservation state of the rail tunnel/bridge.
Definition: tunnelbridge_map.h:91
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:116
ship.h
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95
RailTypeInfo
This struct contains all the info that is needed to draw and construct tracks.
Definition: rail.h:127
VETSB_CONTINUE
@ VETSB_CONTINUE
Bit sets of the above specified bits.
Definition: tile_cmd.h:35
IsTunnelInWayDir
bool IsTunnelInWayDir(TileIndex tile, int z, DiagDirection dir)
Is there a tunnel in the way in the given direction?
Definition: tunnel_map.cpp:48
TUNNEL_SOUND_FRAME
static const uint8_t TUNNEL_SOUND_FRAME
Frame when the 'enter tunnel' sound should be played.
Definition: tunnelbridge_cmd.cpp:1883
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:49
ObjectSpec::height
uint8_t height
The height of this structure, in heightlevels; max MAX_TILE_HEIGHT.
Definition: newgrf_object.h:74
BB_HEIGHT_UNDER_BRIDGE
static const uint BB_HEIGHT_UNDER_BRIDGE
Some values for constructing bounding boxes (BB).
Definition: viewport_type.h:88
GetPartialPixelZ
uint GetPartialPixelZ(int x, int y, Slope corners)
Determines height at given coordinate of a slope.
Definition: landscape.cpp:228
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
TILE_SIZE
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
ClearBridgeMiddle
void ClearBridgeMiddle(Tile t)
Removes bridges from the given, that is bridges along the X and Y axis.
Definition: bridge_map.h:103
SLOPE_NW
@ SLOPE_NW
north and west corner are raised
Definition: slope_type.h:55
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
Town::xy
TileIndex xy
town center tile
Definition: town.h:55
DrawRailCatenaryOnBridge
void DrawRailCatenaryOnBridge(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition: elrail.cpp:506
town.h
TileInfo::y
int y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:45
BridgeSpec::speed
uint16_t speed
maximum travel speed (1 unit = 1/1.6 mph = 1 km-ish/h)
Definition: bridge.h:47
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Company::infrastructure
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
Definition: company_base.h:147
DrawBridgeMiddle
void DrawBridgeMiddle(const TileInfo *ti)
Draw the middle bits of a bridge.
Definition: tunnelbridge_cmd.cpp:1541
CheckAllowRemoveTunnelBridge
static CommandCost CheckAllowRemoveTunnelBridge(TileIndex tile)
Are we allowed to remove the tunnel or bridge at tile?
Definition: tunnelbridge_cmd.cpp:797
ConstructionSettings::max_tunnel_length
uint16_t max_tunnel_length
maximum length of tunnels
Definition: settings_type.h:387
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:244
RailTypeInfo::strings
struct RailTypeInfo::@26 strings
Strings associated with the rail type.
GetRailType
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition: rail_map.h:115
ConstructionSettings::max_bridge_length
uint16_t max_bridge_length
maximum length of bridges
Definition: settings_type.h:385
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:376
GetSlopePixelZ
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
Definition: landscape.cpp:303
SubSprite
Used to only draw a part of the sprite.
Definition: gfx_type.h:231
CheckforTownRating
CommandCost CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type)
Does the town authority allow the (destructive) action of the current company?
Definition: town_cmd.cpp:3980
GameCreationSettings::landscape
uint8_t landscape
the landscape we're currently in
Definition: settings_type.h:368
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:50
TileDesc
Tile description for the 'land area information' tool.
Definition: tile_cmd.h:52
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:374
Foundation
Foundation
Enumeration for Foundations.
Definition: slope_type.h:93
Vehicle::vehstatus
uint8_t vehstatus
Status.
Definition: vehicle_base.h:354
CommandCost::Succeeded
bool Succeeded() const
Did this command succeed?
Definition: command_type.h:162
object_base.h
pbs.h
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:594
TRANSPORT_ROAD
@ TRANSPORT_ROAD
Transport by road vehicle.
Definition: transport_type.h:28
IsCoast
bool IsCoast(Tile t)
Is it a coast tile?
Definition: water_map.h:204
DiagDirToDiagTrackBits
TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track bits incidating with that diagdir.
Definition: track_func.h:524
ComplementSlope
Slope ComplementSlope(Slope s)
Return the complement of a slope.
Definition: slope_func.h:76
TileInfo::tileh
Slope tileh
Slope of the tile.
Definition: tile_cmd.h:46
GVF_GOINGDOWN_BIT
@ GVF_GOINGDOWN_BIT
Vehicle is currently going downhill. (Cached track information for acceleration)
Definition: ground_vehicle.hpp:56
GetTileType
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
RTO_SLOPE_NE
@ RTO_SLOPE_NE
Piece of rail on slope with north-east raised.
Definition: rail.h:77
MakeRailBridgeRamp
void MakeRailBridgeRamp(Tile t, Owner o, BridgeType bridgetype, DiagDirection d, RailType rt)
Make a bridge ramp for rails.
Definition: bridge_map.h:169
BridgeSpec
Struct containing information about a single bridge type.
Definition: bridge.h:42
DrawPillarColumn
static int DrawPillarColumn(int z_bottom, int z_top, const PalSpriteID *psid, int x, int y, int w, int h)
Draw two bridge pillars (north and south).
Definition: tunnelbridge_cmd.cpp:1054
IsInvisibilitySet
bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:59
RoadBuildCost
Money RoadBuildCost(RoadType roadtype)
Returns the cost of building the specified roadtype.
Definition: road.h:252
RailTypeInfo::name
StringID name
Name of this rail type.
Definition: rail.h:176
GetCustomRoadSprite
SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
Definition: newgrf_roadtype.cpp:101
SetBridgeMiddle
void SetBridgeMiddle(Tile t, Axis a)
Set that there is a bridge over the given axis.
Definition: bridge_map.h:114
TO_BRIDGES
@ TO_BRIDGES
bridges
Definition: transparency.h:28
TransportType
TransportType
Available types of transport.
Definition: transport_type.h:19
RTBO_Y
@ RTBO_Y
Piece of rail in Y direction.
Definition: rail.h:94
CheckForDockingTile
void CheckForDockingTile(TileIndex t)
Mark the supplied tile as a docking tile if it is suitable for docking.
Definition: water_cmd.cpp:184
VS_HIDDEN
@ VS_HIDDEN
Vehicle is not visible.
Definition: vehicle_base.h:33
DIAGDIR_NW
@ DIAGDIR_NW
Northwest.
Definition: direction_type.h:78
landscape_cmd.h
RailVehicleInfo::engclass
EngineClass engclass
Class of engine for this vehicle.
Definition: engine_type.h:53
TILE_UNIT_MASK
static const uint TILE_UNIT_MASK
For masking in/out the inner-tile world coordinate units.
Definition: tile_type.h:16
CheckOwnership
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
Definition: company_cmd.cpp:363
return_cmd_error
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:38
TrackBitsToTrackdirBits
TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
Definition: track_func.h:319
DIAGDIR_SE
@ DIAGDIR_SE
Southeast.
Definition: direction_type.h:76
HasRoadCatenaryDrawn
bool HasRoadCatenaryDrawn(RoadType roadtype)
Test if we should draw road catenary.
Definition: road_func.h:145
CommandCost
Common return value for all commands.
Definition: command_type.h:23
IsBridge
bool IsBridge(Tile t)
Checks if this is a bridge, instead of a tunnel.
Definition: bridge_map.h:24
GetBridgeSpec
const BridgeSpec * GetBridgeSpec(BridgeType i)
Get the specification of a bridge type.
Definition: bridge.h:66
ApplyPixelFoundationToSlope
uint ApplyPixelFoundationToSlope(Foundation f, Slope &s)
Applies a foundation to a slope.
Definition: landscape.h:126
IsWater
bool IsWater(Tile t)
Is it a plain water tile?
Definition: water_map.h:150
clear_func.h
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
MAX_BRIDGES
static const uint MAX_BRIDGES
Maximal number of available bridge specs.
Definition: bridge.h:35
RTSG_BRIDGE
@ RTSG_BRIDGE
Bridge surface images.
Definition: rail.h:56
CmdBuildBridge
CommandCost CmdBuildBridge(DoCommandFlag flags, TileIndex tile_end, TileIndex tile_start, TransportType transport_type, BridgeType bridge_type, uint8_t road_rail_type)
Build a Bridge.
Definition: tunnelbridge_cmd.cpp:261
INVALID_DIAGDIR
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Definition: direction_type.h:80
DirtyCompanyInfrastructureWindows
void DirtyCompanyInfrastructureWindows(CompanyID company)
Redraw all windows with company infrastructure counts.
Definition: company_gui.cpp:2584
_bridge
BridgeSpec _bridge[MAX_BRIDGES]
The specification of all bridges.
Definition: tunnelbridge_cmd.cpp:52
CalcClosestTownFromTile
Town * CalcClosestTownFromTile(TileIndex tile, uint threshold=UINT_MAX)
Return the town closest to the given tile within threshold.
Definition: town_cmd.cpp:3846
GVF_GOINGUP_BIT
@ GVF_GOINGUP_BIT
Vehicle is currently going uphill. (Cached track information for acceleration)
Definition: ground_vehicle.hpp:55
GetTilePixelSlope
std::tuple< Slope, int > GetTilePixelSlope(TileIndex tile)
Return the slope of a given tile.
Definition: tile_map.h:289
HasTunnelBridgeSnowOrDesert
bool HasTunnelBridgeSnowOrDesert(Tile t)
Tunnel: Is this tunnel entrance in a snowy or desert area? Bridge: Does the bridge ramp lie in a snow...
Definition: tunnelbridge_map.h:52
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:264
MakeRoadTunnel
void MakeRoadTunnel(Tile t, Owner o, DiagDirection d, RoadType road_rt, RoadType tram_rt)
Makes a road tunnel entrance.
Definition: tunnel_map.h:50
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:340
autoslope.h
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:323
TileDesc::roadtype
StringID roadtype
Type of road on the tile.
Definition: tile_cmd.h:66
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:58
IsTunnel
bool IsTunnel(Tile t)
Is this a tunnel (entrance)?
Definition: tunnel_map.h:23
_cheats
Cheats _cheats
All the cheats.
Definition: cheat.cpp:16
ClearedObjectArea::first_tile
TileIndex first_tile
The first tile being cleared, which then causes the whole object to be cleared.
Definition: object_base.h:85
TRACKDIR_BIT_NONE
@ TRACKDIR_BIT_NONE
No track build.
Definition: track_type.h:99
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:54
ReverseDiagDir
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Definition: direction_func.h:118
CommandCost::Failed
bool Failed() const
Did this command fail?
Definition: command_type.h:171
station_func.h
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
_tunnel_visibility_frame
const uint8_t _tunnel_visibility_frame[DIAGDIR_END]
Frame when a vehicle should be hidden in a tunnel with a certain direction.
SetRoadside
void SetRoadside(Tile tile, Roadside s)
Set the decorations of a road.
Definition: road_map.h:503
GetNorthernBridgeEnd
TileIndex GetNorthernBridgeEnd(TileIndex t)
Finds the northern end of a bridge starting at a middle tile.
Definition: bridge_map.cpp:39
CheckBridgeSlope
static CommandCost CheckBridgeSlope(BridgePieces bridge_piece, Axis axis, Slope &tileh, int &z)
Determines the foundation for the bridge head, and tests if the resulting slope is valid.
Definition: tunnelbridge_cmd.cpp:173
AxisToDiagDir
DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
Definition: direction_func.h:232
EndSpriteCombine
void EndSpriteCombine()
Terminates a block of sprites started by StartSpriteCombine.
Definition: viewport.cpp:779
DIAGDIR_SW
@ DIAGDIR_SW
Southwest.
Definition: direction_type.h:77
TUNNELBRIDGE_TRACKBIT_FACTOR
static const uint TUNNELBRIDGE_TRACKBIT_FACTOR
Multiplier for how many regular track bits a tunnel/bridge counts.
Definition: economy_type.h:241
Vehicle::cur_speed
uint16_t cur_speed
current speed
Definition: vehicle_base.h:328
water_regions.h
TileDiffXY
TileIndexDiff TileDiffXY(int x, int y)
Calculates an offset for the given coordinate(-offset).
Definition: map_func.h:401
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
GetSlopePixelZOnEdge
void GetSlopePixelZOnEdge(Slope tileh, DiagDirection edge, int &z1, int &z2)
Determine the Z height of the corners of a specific tile edge.
Definition: landscape.cpp:354
ApplyFoundationToSlope
uint ApplyFoundationToSlope(Foundation f, Slope &s)
Applies a foundation to a slope.
Definition: landscape.cpp:170
IsBridgeTile
bool IsBridgeTile(Tile t)
checks if there is a bridge on this tile
Definition: bridge_map.h:35
RoadType
RoadType
The different roadtypes we support.
Definition: road_type.h:25
MakeRailTunnel
void MakeRailTunnel(Tile t, Owner o, DiagDirection d, RailType r)
Makes a rail tunnel entrance.
Definition: tunnel_map.h:73
SetTunnelBridgeSnowOrDesert
void SetTunnelBridgeSnowOrDesert(Tile t, bool snow_or_desert)
Tunnel: Places this tunnel entrance in a snowy or desert area, or takes it out of there.
Definition: tunnelbridge_map.h:66
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:52
safeguards.h
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:286
IsNormalRoadTile
static debug_inline bool IsNormalRoadTile(Tile t)
Return whether a tile is a normal road tile.
Definition: road_map.h:74
Train
'Train' is either a loco or a wagon.
Definition: train.h:89
FreeTrainTrackReservation
void FreeTrainTrackReservation(const Train *v)
Free the reserved path in front of a vehicle.
Definition: train_cmd.cpp:2391
HasTileRoadType
bool HasTileRoadType(Tile t, RoadTramType rtt)
Check if a tile has a road or a tram road type.
Definition: road_map.h:211
CommandCost::GetCost
Money GetCost() const
The costs as made up to this moment.
Definition: command_type.h:83
DirToDiagDir
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Definition: direction_func.h:166
HasSlopeHighestCorner
bool HasSlopeHighestCorner(Slope s)
Tests if a slope has a highest corner (i.e.
Definition: slope_func.h:113
TCX_NORMAL
@ TCX_NORMAL
Nothing special.
Definition: newgrf_commons.h:24
ROTSG_BRIDGE
@ ROTSG_BRIDGE
Required: Bridge surface images.
Definition: road.h:66
GetTileOwner
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition: tile_map.h:178
StartSpriteCombine
void StartSpriteCombine()
Starts a block of sprites, which are "combined" into a single bounding box.
Definition: viewport.cpp:769
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:57
INVALID_ROADTYPE
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition: road_type.h:30
TileIndexDiff
int32_t TileIndexDiff
An offset value between two tiles.
Definition: map_func.h:376
FOUNDATION_NONE
@ FOUNDATION_NONE
The tile has no foundation, the slope remains unchanged.
Definition: slope_type.h:94
RoadTypeInfo::name
StringID name
Name of this rail type.
Definition: road.h:103
ValParamRoadType
bool ValParamRoadType(RoadType roadtype)
Validate functions for rail building.
Definition: road.cpp:153
GetSouthernBridgeEnd
TileIndex GetSouthernBridgeEnd(TileIndex t)
Finds the southern end of a bridge starting at a middle tile.
Definition: bridge_map.cpp:49
RTBO_SLOPE
@ RTBO_SLOPE
Sloped rail pieces, in order NE, SE, SW, NW.
Definition: rail.h:95
CombineTrackStatus
TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
Builds a TrackStatus.
Definition: track_func.h:388
tunnelbridge_cmd.h
PrepareToEnterBridge
static void PrepareToEnterBridge(T *gv)
Helper to prepare the ground vehicle when entering a bridge.
Definition: tunnelbridge_cmd.cpp:1868
CommandCost::AddCost
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Definition: command_type.h:63
GUISettings::show_track_reservation
bool show_track_reservation
highlight reserved tracks.
Definition: settings_type.h:193
BridgeType
uint BridgeType
Bridge spec number.
Definition: bridge.h:37
stdafx.h
DrawRoadOverlays
void DrawRoadOverlays(const TileInfo *ti, PaletteID pal, const RoadTypeInfo *road_rti, const RoadTypeInfo *tram_rti, uint road_offset, uint tram_offset, bool draw_underlay)
Draw road underlay and overlay sprites.
Definition: road_cmd.cpp:1515
TileTypeProcs
Set of callback functions for performing tile operations of a given tile type.
Definition: tile_cmd.h:158
ConstructionSettings::extra_dynamite
bool extra_dynamite
extra dynamite
Definition: settings_type.h:389
GetBridgeFoundation
Foundation GetBridgeFoundation(Slope tileh, Axis axis)
Get the foundation for a bridge.
Definition: tunnelbridge_cmd.cpp:129
SetTileOwner
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition: tile_map.h:198
CalcBridgeLenCostFactor
int CalcBridgeLenCostFactor(int length)
Calculate the price factor for building a long bridge.
Definition: tunnelbridge_cmd.cpp:108
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
GetBridgeRoadCatenary
static void GetBridgeRoadCatenary(const RoadTypeInfo *rti, TileIndex head_tile, int offset, bool head, SpriteID &spr_back, SpriteID &spr_front)
Retrieve the sprites required for catenary on a road/tram bridge.
Definition: tunnelbridge_cmd.cpp:1130
SPR_RAILTYPE_TUNNEL_BASE
static const SpriteID SPR_RAILTYPE_TUNNEL_BASE
Tunnel sprites with grass only for custom railtype tunnel.
Definition: sprites.h:299
Cheat::value
bool value
tells if the bool cheat is active or not
Definition: cheat_type.h:18
DC_BANKRUPT
@ DC_BANKRUPT
company bankrupts, skip money check, skip vehicle on tile check in some cases
Definition: command_type.h:382
CompanyInfrastructure::rail
std::array< uint32_t, RAILTYPE_END > rail
Count of company owned track bits for each rail type.
Definition: company_base.h:33
RTSG_OVERLAY
@ RTSG_OVERLAY
Images for overlaying track.
Definition: rail.h:51
viewport_func.h
DoClearBridge
static CommandCost DoClearBridge(TileIndex tile, DoCommandFlag flags)
Remove a bridge from the game, update town rating, etc.
Definition: tunnelbridge_cmd.cpp:925
OWNER_TOWN
@ OWNER_TOWN
A town owns the tile, or a town is expanding.
Definition: company_type.h:24
RoadClearCost
Money RoadClearCost(RoadType roadtype)
Returns the cost of clearing the specified roadtype.
Definition: road.h:263
HasTileWaterClass
bool HasTileWaterClass(Tile t)
Checks whether the tile has an waterclass associated.
Definition: water_map.h:104
ChangeTownRating
void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags)
Changes town rating of the current company.
Definition: town_cmd.cpp:3943
Vehicle::z_pos
int32_t z_pos
z coordinate.
Definition: vehicle_base.h:306
AddSortableSpriteToDraw
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition: viewport.cpp:673
RailTypeInfo::single_sloped
SpriteID single_sloped
single piece of rail for slopes
Definition: rail.h:143
AXIS_X
@ AXIS_X
The X axis.
Definition: direction_type.h:117
IsValidTile
bool IsValidTile(Tile tile)
Checks if a tile is valid.
Definition: tile_map.h:161
yapf_cache.h
TRACK_BIT_WORMHOLE
@ TRACK_BIT_WORMHOLE
Bitflag for a wormhole (used for tunnels)
Definition: track_type.h:52
Vehicle::direction
Direction direction
facing
Definition: vehicle_base.h:307
newgrf_object.h
RailTypeInfo::max_speed
uint16_t max_speed
Maximum speed for vehicles travelling on this rail type.
Definition: rail.h:231
TileOffsByDiagDir
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:565
RailTypeInfo::single_x
SpriteID single_x
single piece of rail in X direction, without ground
Definition: rail.h:137
GetRoadTypeInfo
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:227
DrawFoundation
void DrawFoundation(TileInfo *ti, Foundation f)
Draw foundation f at tile ti.
Definition: landscape.cpp:425
DistanceSquare
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the 'Square' distance between the two given tiles.
Definition: map.cpp:157
RTSG_TUNNEL
@ RTSG_TUNNEL
Main group of ground images for snow or desert.
Definition: rail.h:53
TileDesc::tram_speed
uint16_t tram_speed
Speed limit of tram (bridges and track)
Definition: tile_cmd.h:69
Ship
All ships have this type.
Definition: ship.h:24
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:53
vehicle_func.h
Track
Track
These are used to specify a single track.
Definition: track_type.h:19
newgrf_sound.h
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1610
strings_func.h
RTBO_X
@ RTBO_X
Piece of rail in X direction.
Definition: rail.h:93
Vehicle::First
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:645
newgrf_roadtype.h
FindClearedObject
ClearedObjectArea * FindClearedObject(TileIndex tile)
Find the entry in _cleared_object_areas which occupies a certain tile.
Definition: object_cmd.cpp:530
ClearTile_TunnelBridge
static CommandCost ClearTile_TunnelBridge(TileIndex tile, DoCommandFlag flags)
Remove a tunnel or a bridge from the game.
Definition: tunnelbridge_cmd.cpp:1016
CmdBuildTunnel
CommandCost CmdBuildTunnel(DoCommandFlag flags, TileIndex start_tile, TransportType transport_type, uint8_t road_rail_type)
Build Tunnel.
Definition: tunnelbridge_cmd.cpp:623
Slope
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
TryPathReserve
bool TryPathReserve(Train *v, bool mark_as_stuck=false, bool first_tile_okay=false)
Try to reserve a path to a safe position.
Definition: train_cmd.cpp:2871
SpecializedVehicle< Train, Type >::From
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1215
Axis
Axis
Allow incrementing of DiagDirDiff variables.
Definition: direction_type.h:116
abs
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:23
TRANSPORT_RAIL
@ TRANSPORT_RAIL
Transport by train.
Definition: transport_type.h:27
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
TCX_ON_BRIDGE
@ TCX_ON_BRIDGE
Querying information about stuff on the bridge (via some bridgehead).
Definition: newgrf_commons.h:26
ConstructionSettings::build_on_slopes
bool build_on_slopes
allow building on slopes
Definition: settings_type.h:383
ROTSG_OVERLAY
@ ROTSG_OVERLAY
Optional: Images for overlaying track.
Definition: road.h:61
RTO_X
@ RTO_X
Piece of rail in X direction.
Definition: rail.h:71
OWNER_WATER
@ OWNER_WATER
The tile/execution is done by "water".
Definition: company_type.h:26
cheat_type.h
SLOPE_SW
@ SLOPE_SW
south and west corner are raised
Definition: slope_type.h:56
MarkTileDirtyByTile
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:2054
ObjectSpec::GetByTile
static const ObjectSpec * GetByTile(TileIndex tile)
Get the specification associated with a tile.
Definition: newgrf_object.cpp:65
AddSideToSignalBuffer
void AddSideToSignalBuffer(TileIndex tile, DiagDirection side, Owner owner)
Add side of tile to signal update buffer.
Definition: signal.cpp:624
CompanyInfrastructure::road
std::array< uint32_t, ROADTYPE_END > road
Count of company owned track bits for each road type.
Definition: company_base.h:34
Ship::state
TrackBits state
The "track" the ship is following.
Definition: ship.h:26
GetRoadOwner
Owner GetRoadOwner(Tile t, RoadTramType rtt)
Get the owner of a specific road type.
Definition: road_map.h:234
ROTSG_CATENARY_FRONT
@ ROTSG_CATENARY_FRONT
Optional: Catenary front.
Definition: road.h:64
Town::cache
TownCache cache
Container for all cacheable data.
Definition: town.h:57
UpdateCompanyRoadInfrastructure
void UpdateCompanyRoadInfrastructure(RoadType rt, Owner o, int count)
Update road infrastructure counts for a company.
Definition: road_cmd.cpp:190
HasRailCatenaryDrawn
bool HasRailCatenaryDrawn(RailType rt)
Test if we should draw rail catenary.
Definition: elrail_func.h:30
GetTunnelBridgeTransportType
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
Definition: tunnelbridge_map.h:39
DIAGDIR_END
@ DIAGDIR_END
Used for iterations.
Definition: direction_type.h:79
IsBridgeAbove
bool IsBridgeAbove(Tile t)
checks if a bridge is set above the ground of this tile
Definition: bridge_map.h:45
TileDesc::str
StringID str
Description of the tile.
Definition: tile_cmd.h:53
DC_AUTO
@ DC_AUTO
don't allow building on structures
Definition: command_type.h:377
PalSpriteID
Combination of a palette sprite and a 'real' sprite.
Definition: gfx_type.h:23
DiagDirToDiagTrack
Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track incidating with that diagdir.
Definition: track_func.h:512
RoadTypeInfo::strings
struct RoadTypeInfo::@29 strings
Strings associated with the rail type.
SetRoadOwner
void SetRoadOwner(Tile t, RoadTramType rtt, Owner o)
Set the owner of a specific road type.
Definition: road_map.h:251
GetOtherTunnelBridgeEnd
TileIndex GetOtherTunnelBridgeEnd(Tile t)
Determines type of the wormhole and returns its other end.
Definition: tunnelbridge_map.h:78
AxisToTrack
Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track AXIS_X -> TRACK_X AXIS_Y -> TRACK_Y Uses the fact that the...
Definition: track_func.h:66
ROTSG_TUNNEL
@ ROTSG_TUNNEL
Optional: Ground images for tunnels.
Definition: road.h:63
GetOtherTunnelEnd
TileIndex GetOtherTunnelEnd(TileIndex tile)
Gets the other end of the tunnel.
Definition: tunnel_map.cpp:22
PlayVehicleSound
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event, bool force)
Checks whether a NewGRF wants to play a different vehicle sound effect.
Definition: newgrf_sound.cpp:187
TileArea
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Definition: tilearea_type.h:102
DrawRailCatenaryOnTunnel
void DrawRailCatenaryOnTunnel(const TileInfo *ti)
Draws wires on a tunnel tile.
Definition: elrail.cpp:249
MarkBridgeDirty
void MarkBridgeDirty(TileIndex begin, TileIndex end, DiagDirection direction, uint bridge_height)
Mark bridge tiles dirty.
Definition: tunnelbridge_cmd.cpp:67
CalcBridgePiece
static BridgePieces CalcBridgePiece(uint north, uint south)
Compute bridge piece.
Definition: tunnelbridge_cmd.cpp:1522
CheckTileOwnership
CommandCost CheckTileOwnership(TileIndex tile)
Check whether the current owner owns the stuff on the given tile.
Definition: company_cmd.cpp:381
CommandHelper
Definition: command_func.h:93
ConstructionSettings::max_bridge_height
uint8_t max_bridge_height
maximum height of bridges
Definition: settings_type.h:386
BridgeSpec::min_length
uint8_t min_length
the minimum length (not counting start and end tile)
Definition: bridge.h:44
RoadTypeInfo::max_speed
uint16_t max_speed
Maximum speed for vehicles travelling on this road type.
Definition: road.h:142
Town
Town data structure.
Definition: town.h:54
RVSB_WORMHOLE
@ RVSB_WORMHOLE
The vehicle is in a tunnel and/or bridge.
Definition: roadveh.h:39
SlopeToSpriteOffset
uint SlopeToSpriteOffset(Slope s)
Returns the Sprite offset for a given Slope.
Definition: slope_func.h:415
BridgeSpec::avail_year
TimerGameCalendar::Year avail_year
the year where it becomes available
Definition: bridge.h:43
TileHeight
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
Definition: tile_map.h:29
TownCache::squared_town_zone_radius
std::array< uint32_t, HZB_END > squared_town_zone_radius
UpdateTownRadius updates this given the house count.
Definition: town.h:47
RailTypeInfo::base_sprites
struct RailTypeInfo::@23 base_sprites
Struct containing the main sprites.
TILE_HEIGHT
static const uint TILE_HEIGHT
Height of a height level in world coordinate AND in pixels in #ZOOM_BASE.
Definition: tile_type.h:18
OverflowSafeInt< int64_t >
DrawTile_TunnelBridge
static void DrawTile_TunnelBridge(TileInfo *ti)
Draws a tunnel of bridge tile.
Definition: tunnelbridge_cmd.cpp:1256
DrawRailCatenary
void DrawRailCatenary(const TileInfo *ti)
Draws overhead wires and pylons for electric railways.
Definition: elrail.cpp:568
CheckBridgeAvailability
CommandCost CheckBridgeAvailability(BridgeType bridge_type, uint bridge_len, DoCommandFlag flags)
Is a bridge of the specified type and length available?
Definition: tunnelbridge_cmd.cpp:200
MakeRoadBridgeRamp
void MakeRoadBridgeRamp(Tile t, Owner o, Owner owner_road, Owner owner_tram, BridgeType bridgetype, DiagDirection d, RoadType road_rt, RoadType tram_rt)
Make a bridge ramp for roads.
Definition: bridge_map.h:153
TileDesc::owner_type
StringID owner_type[4]
Type of each owner.
Definition: tile_cmd.h:56
TileInfo::x
int x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:44
PalSpriteID::pal
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition: gfx_type.h:25
TileInfo::tile
TileIndex tile
Tile index.
Definition: tile_cmd.h:47
GameSettings::construction
ConstructionSettings construction
construction of things in-game
Definition: settings_type.h:595
ROADSIDE_PAVED
@ ROADSIDE_PAVED
Road with paved sidewalks.
Definition: road_map.h:480
TileDesc::rail_speed
uint16_t rail_speed
Speed limit of rail (bridges and track)
Definition: tile_cmd.h:65
GetSnowLine
uint8_t GetSnowLine()
Get the current snow line, either variable or static.
Definition: landscape.cpp:608
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:328
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:51
MakeAqueductBridgeRamp
void MakeAqueductBridgeRamp(Tile t, Owner o, DiagDirection d)
Make a bridge ramp for aqueducts.
Definition: bridge_map.h:181
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
TileDesc::tramtype
StringID tramtype
Type of tram on the tile.
Definition: tile_cmd.h:68
DC_QUERY_COST
@ DC_QUERY_COST
query cost only, don't build.
Definition: command_type.h:378
GetBridgePixelHeight
int GetBridgePixelHeight(TileIndex tile)
Get the height ('z') of a bridge in pixels.
Definition: bridge_map.h:84
TUNNELBRIDGE_REMOVE
@ TUNNELBRIDGE_REMOVE
Removal of a tunnel or bridge owned by the towb.
Definition: town.h:177
AXIS_Y
@ AXIS_Y
The y axis.
Definition: direction_type.h:118
ClearedObjectArea
Keeps track of removed objects during execution/testruns of commands.
Definition: object_base.h:84
TunnelBridgeClearCost
static Money TunnelBridgeClearCost(TileIndex tile, Price base_price)
Calculate the base cost of clearing a tunnel/bridge per tile.
Definition: tunnelbridge_cmd.cpp:224
GetBridgeAxis
Axis GetBridgeAxis(Tile t)
Get the axis of the bridge that goes over the tile.
Definition: bridge_map.h:68
Company
Definition: company_base.h:133
BridgePieces
BridgePieces
This enum is related to the definition of bridge pieces, which is used to determine the proper sprite...
Definition: bridge.h:22
ClrBit
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
IsTileOwner
bool IsTileOwner(Tile tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:214
DiagDirToDiagTrackdir
Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition: track_func.h:537
GetTropicZone
TropicZone GetTropicZone(Tile tile)
Get the tropic zone.
Definition: tile_map.h:238
OtherAxis
Axis OtherAxis(Axis a)
Select the other axis as provided.
Definition: direction_func.h:197
ROTSG_CATENARY_BACK
@ ROTSG_CATENARY_BACK
Optional: Catenary back.
Definition: road.h:65
DrawBridgePillars
static void DrawBridgePillars(const PalSpriteID *psid, const TileInfo *ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge)
Draws the pillars under high bridges.
Definition: tunnelbridge_cmd.cpp:1074
bridge_land.h
GetInclinedSlopeDirection
DiagDirection GetInclinedSlopeDirection(Slope s)
Returns the direction of an inclined slope.
Definition: slope_func.h:239
HasTileWaterGround
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition: water_map.h:353
CompanyInfrastructure::water
uint32_t water
Count of company owned track bits for canals.
Definition: company_base.h:36
GetTileSlopeZ
std::tuple< Slope, int > GetTileSlopeZ(TileIndex tile)
Return the slope of a given tile inside the map.
Definition: tile_map.cpp:55
IsRoadDepot
static debug_inline bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition: road_map.h:106
RoadVehicle::state
uint8_t state
Definition: roadveh.h:108
RATING_TUNNEL_BRIDGE_DOWN_STEP
@ RATING_TUNNEL_BRIDGE_DOWN_STEP
penalty for removing town owned tunnel or bridge
Definition: town_type.h:57
RailTypeInfo::single_y
SpriteID single_y
single piece of rail in Y direction, without ground
Definition: rail.h:138
VehicleEnterTileStatus
VehicleEnterTileStatus
The returned bits of VehicleEnterTile.
Definition: tile_cmd.h:21
newgrf_railtype.h
TunnelBridgeIsFree
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore)
Finds vehicle in tunnel / bridge.
Definition: vehicle.cpp:575
DrawGroundSprite
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.
Definition: viewport.cpp:589
_build_tunnel_endtile
TileIndex _build_tunnel_endtile
The end of a tunnel; as hidden return from the tunnel build command for GUI purposes.
Definition: tunnelbridge_cmd.cpp:53
GetBridgeType
BridgeType GetBridgeType(Tile t)
Determines the type of bridge on a tile.
Definition: bridge_map.h:56
RailBuildCost
Money RailBuildCost(RailType railtype)
Returns the cost of building the specified railtype.
Definition: rail.h:375
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:611
GetTrainForReservation
Train * GetTrainForReservation(TileIndex tile, Track track)
Find the train which has reserved a specific path.
Definition: pbs.cpp:330
BridgeSpec::max_length
uint16_t max_length
the maximum length (not counting start and end tile)
Definition: bridge.h:45
GetCustomRailSprite
SpriteID GetCustomRailSprite(const RailTypeInfo *rti, TileIndex tile, RailTypeSpriteGroup rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
Definition: newgrf_railtype.cpp:96
SPR_TRAMWAY_BASE
static const SpriteID SPR_TRAMWAY_BASE
Tramway sprites.
Definition: sprites.h:272
DrawBridgeRoadBits
static void DrawBridgeRoadBits(TileIndex head_tile, int x, int y, int z, int offset, bool head)
Draws the road and trambits over an already drawn (lower end) of a bridge.
Definition: tunnelbridge_cmd.cpp:1156
VETSB_ENTERED_WORMHOLE
@ VETSB_ENTERED_WORMHOLE
The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/...
Definition: tile_cmd.h:37
roadveh.h
GetTunnelBridgeDirection
DiagDirection GetTunnelBridgeDirection(Tile t)
Get the direction pointing to the other end.
Definition: tunnelbridge_map.h:26
TimerGameCalendar::year
static Year year
Current year, starting at 0.
Definition: timer_game_calendar.h:32
ObjectSpec::flags
ObjectFlags flags
Flags/settings related to the object.
Definition: newgrf_object.h:72
EXPENSES_CONSTRUCTION
@ EXPENSES_CONSTRUCTION
Construction costs.
Definition: economy_type.h:173
RTSG_TUNNEL_PORTAL
@ RTSG_TUNNEL_PORTAL
Tunnel portal overlay.
Definition: rail.h:60
HasBit
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103