OpenTTD Source 20261112-master-gbcbec5e63f
waypoint_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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11
12#include "command_func.h"
13#include "landscape.h"
14#include "town.h"
15#include "waypoint_base.h"
17#include "tilehighlight_func.h"
18#include "strings_func.h"
19#include "viewport_func.h"
20#include "viewport_kdtree.h"
21#include "station_func.h"
22#include "station_kdtree.h"
23#include "window_func.h"
25#include "vehicle_func.h"
26#include "string_func.h"
27#include "company_func.h"
28#include "newgrf_debug.h"
29#include "newgrf_station.h"
30#include "newgrf_roadstop.h"
31#include "company_base.h"
32#include "water.h"
33#include "company_gui.h"
34#include "waypoint_cmd.h"
35#include "landscape_cmd.h"
36#include "station_layout_type.h"
37
38#include "table/strings.h"
39
40#include "safeguards.h"
41
46{
47 Point pt = RemapCoords2(TileX(this->xy) * TILE_SIZE, TileY(this->xy) * TILE_SIZE);
48 if (this->sign.kdtree_valid) _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeWaypoint(this->index));
49
50 this->sign.UpdatePosition(pt.x, pt.y - 32 * ZOOM_BASE, GetString(STR_WAYPOINT_NAME, this->index));
51
52 _viewport_sign_kdtree.Insert(ViewportSignKdtreeItem::MakeWaypoint(this->index));
53
54 /* Recenter viewport */
55 InvalidateWindowData(WindowClass::WaypointView, this->index);
56}
57
63{
64 if (this->xy == new_xy) return;
65
66 this->BaseStation::MoveSign(new_xy);
67}
68
77static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile, StringID str, CompanyID cid, bool is_road)
78{
79 Waypoint *best = nullptr;
80 uint thres = 8;
81
82 for (Waypoint *wp : Waypoint::Iterate()) {
83 if (!wp->IsInUse() && wp->string_id == str && wp->owner == cid && HasBit(wp->waypoint_flags, WPF_ROAD) == is_road) {
84 uint cur_dist = DistanceManhattan(tile, wp->xy);
85
86 if (cur_dist < thres) {
87 thres = cur_dist;
88 best = wp;
89 }
90 }
91 }
92
93 return best;
94}
95
104{
105 /* The axis for rail waypoints is easy. */
106 if (IsRailWaypointTile(tile)) return GetRailStationAxis(tile);
107
108 /* Non-plain rail type, no valid axis for waypoints. */
110
111 TrackBits bits = GetTrackBits(tile);
112 if (bits == Track::X) return Axis::X;
113 if (bits == Track::Y) return Axis::Y;
114 return Axis::Invalid;
115}
116
125{
126 /* The axis for existing road waypoints is easy. */
127 if (IsRoadWaypointTile(tile)) return GetDriveThroughStopAxis(tile);
128
129 /* Non-plain road type, no valid axis for waypoints. */
130 if (!IsNormalRoadTile(tile)) return Axis::Invalid;
131
132 RoadBits bits = GetAllRoadBits(tile);
133
134 if (!bits.Any(ROAD_Y)) return Axis::X;
135 if (!bits.Any(ROAD_X)) return Axis::Y;
136
137 return Axis::Invalid;
138}
139
141
149static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID *waypoint)
150{
151 /* if waypoint is set, then we have special handling to allow building on top of already existing waypoints.
152 * so waypoint points to StationID::Invalid() if we can build on any waypoint.
153 * Or it points to a waypoint if we're only allowed to build on exactly that waypoint. */
154 if (waypoint != nullptr && IsTileType(tile, TileType::Station)) {
155 if (!IsRailWaypoint(tile)) {
156 return ClearTile_Station(tile, DoCommandFlag::Auto); // get error message
157 } else {
158 StationID wp = GetStationIndex(tile);
159 if (*waypoint == StationID::Invalid()) {
160 *waypoint = wp;
161 } else if (*waypoint != wp) {
162 return CommandCost(STR_ERROR_WAYPOINT_ADJOINS_MORE_THAN_ONE_EXISTING);
163 }
164 }
165 }
166
167 if (GetAxisForNewRailWaypoint(tile) != axis) return CommandCost(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
168
169 Owner owner = GetTileOwner(tile);
170 CommandCost ret = CheckOwnership(owner);
171 if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile);
172 if (ret.Failed()) return ret;
173
174 Slope tileh = GetTileSlope(tile);
175 if (tileh != SLOPE_FLAT &&
176 (!_settings_game.construction.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << to_underlying(axis))) || !(tileh & ~(0x3 << to_underlying(axis))))) {
177 return CommandCost(STR_ERROR_FLAT_LAND_REQUIRED);
178 }
179
180 return CommandCost();
181}
182
183extern CommandCost FindJoiningWaypoint(StationID existing_station, StationID station_to_join, bool adjacent, TileArea ta, Waypoint **wp, bool is_road);
184extern CommandCost CalculateRoadStopCost(TileArea tile_area, DoCommandFlags flags, bool is_drive_through, StationType station_type, const RoadStopSpec *roadstopspec, Axis axis, DiagDirection ddir, StationID *est, RoadType rt, Money unit_cost);
187
188extern CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlags flags, int replacement_spec_index);
189
191template <>
193{
194 /* Use predefined layout if it exists. Mask bit zero which will indicate axis. */
195 if (!stl.layout.empty()) return this->stl.layout[this->position] & ~1;
196
197 return 0;
198}
199
214CommandCost CmdBuildRailWaypoint(DoCommandFlags flags, TileIndex start_tile, Axis axis, uint8_t width, uint8_t height, StationClassID spec_class, uint16_t spec_index, StationID station_to_join, bool adjacent)
215{
216 if (!IsValidAxis(axis)) return CMD_ERROR;
217 /* Check if the given station class is valid */
218 if (spec_class.base() >= StationClass::GetClassCount()) return CMD_ERROR;
219 const StationClass *cls = StationClass::Get(spec_class);
220 if (!IsWaypointClass(*cls)) return CMD_ERROR;
221 if (spec_index >= cls->GetSpecCount()) return CMD_ERROR;
222
223 /* The number of parts to build */
224 uint8_t count = axis == Axis::X ? height : width;
225
226 if ((axis == Axis::X ? width : height) != 1) return CMD_ERROR;
227 if (count == 0 || count > _settings_game.station.station_spread) return CMD_ERROR;
228
229 bool reuse = (station_to_join != NEW_STATION);
230 if (!reuse) station_to_join = StationID::Invalid();
231 bool distant_join = (station_to_join != StationID::Invalid());
232
233 if (distant_join && (!_settings_game.station.distant_join_stations || !Waypoint::IsValidID(station_to_join))) return CMD_ERROR;
234
235 TileArea new_location(start_tile, width, height);
236 if (CommandCost ret = CheckStationSpread({}, new_location); ret.Failed()) return ret;
237
238 /* only AddCost for non-existing waypoints */
240 for (TileIndex cur_tile : new_location) {
242 }
243
244 /* Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) */
245 StationID est = StationID::Invalid();
246
247 const StationSpec *spec = StationClass::Get(spec_class)->GetSpec(spec_index);
249
250 /* Check whether the tiles we're building on are valid rail or not. */
251 TileIndexDiff offset = TileOffsByAxis(OtherAxis(axis));
252 for (auto [i, it, tile] = std::make_tuple(0, stl.begin(), start_tile); i < count; ++i, ++it, tile += offset) {
253 CommandCost ret = IsValidTileForWaypoint(tile, axis, &est);
254 if (ret.Failed()) return ret;
255
256 StationGfx gfx = *it + to_underlying(axis);
257 if (spec != nullptr) {
258 uint32_t platinfo = GetPlatformInfo(gfx, count, 1, i, 0, false);
259 /* As the station is not yet completely finished, the station does not yet exist. */
260 uint16_t callback = GetStationCallback(CBID_STATION_BUILD_TILE_LAYOUT, platinfo, 0, spec, nullptr, INVALID_TILE);
261 if (callback != CALLBACK_FAILED && callback <= UINT8_MAX) gfx = (callback & ~1) + to_underlying(axis);
262 }
263
265 if (ret.Failed()) return ret;
266 }
267
268 Waypoint *wp = nullptr;
269 CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, new_location, &wp, false);
270 if (ret.Failed()) return ret;
271
272 /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
273 TileIndex center_tile = start_tile + (count / 2) * offset;
274 if (wp == nullptr && reuse) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT, _current_company, false);
275
276 if (wp != nullptr) {
277 /* Reuse an existing waypoint. */
278 if (wp->owner != _current_company) return CommandCost(STR_ERROR_TOO_CLOSE_TO_ANOTHER_WAYPOINT);
279
280 ret = CheckStationSpread(wp->spread, new_location);
281 if (ret.Failed()) return ret;
282 } else {
283 /* Check if we can create a new waypoint. */
284 if (!Waypoint::CanAllocateItem()) return CommandCost(STR_ERROR_TOO_MANY_STATIONS_LOADING);
285 }
286
287 /* Check if we can allocate a custom spec to this waypoint. */
288 auto specindex = AllocateSpecToStation(spec, wp);
289 if (!specindex.has_value()) return CommandCost(STR_ERROR_TOO_MANY_STATION_SPECS);
290
291 if (flags.Test(DoCommandFlag::Execute)) {
292 if (wp == nullptr) {
293 wp = Waypoint::Create(start_tile);
294 } else if (!wp->IsInUse()) {
295 /* Move existing (recently deleted) waypoint to the new location */
296 wp->xy = start_tile;
297 }
298 wp->owner = GetTileOwner(start_tile);
299
300 wp->spread.Add(new_location);
301 wp->train_station.Add(new_location);
302
303 if (specindex.has_value()) AssignSpecToStation(spec, wp, *specindex);
304
305 wp->delete_ctr = 0;
308 wp->string_id = STR_SV_STNAME_WAYPOINT;
309
310 if (wp->town == nullptr) MakeDefaultName(wp);
311
312 wp->UpdateVirtCoord();
313
314 Company *c = Company::Get(wp->owner);
315 for (auto [i, it, tile] = std::make_tuple(0, stl.begin(), start_tile); i < count; ++i, ++it, tile += offset) {
316 uint8_t old_specindex = HasStationTileRail(tile) ? GetCustomStationSpecIndex(tile) : 0;
318 bool reserved = IsTileType(tile, TileType::Railway) ?
321 MakeRailWaypoint(tile, wp->owner, wp->index, axis, *it, GetRailType(tile));
322 SetCustomStationSpecIndex(tile, *specindex);
323
324 if (spec != nullptr) {
325 uint32_t platinfo = GetPlatformInfo(*it + to_underlying(axis), count, 1, i, 0, false);
326
327 /* As the station is not yet completely finished, the station does not yet exist. */
328 uint16_t callback = GetStationCallback(CBID_STATION_BUILD_TILE_LAYOUT, platinfo, 0, spec, nullptr, tile);
329 if (callback != CALLBACK_FAILED) {
330 if (callback <= UINT8_MAX) {
331 SetStationGfx(tile, (callback & ~1) + to_underlying(axis));
332 } else {
334 }
335 }
336 }
337
338 SetRailStationTileFlags(tile, spec);
339
340 SetRailStationReservation(tile, reserved);
342
343 DeallocateSpecFromStation(wp, old_specindex);
344 if (spec == nullptr) DeleteNewGRFInspectWindow(GrfSpecFeature::Stations, tile);
346 }
348 }
349
350 return cost;
351}
352
366CommandCost CmdBuildRoadWaypoint(DoCommandFlags flags, TileIndex start_tile, Axis axis, uint8_t width, uint8_t height, RoadStopClassID spec_class, uint16_t spec_index, StationID station_to_join, bool adjacent)
367{
368 if (!IsValidAxis(axis)) return CMD_ERROR;
369 /* Check if the given station class is valid */
370 if (spec_class.base() >= RoadStopClass::GetClassCount()) return CMD_ERROR;
371 const RoadStopClass *cls = RoadStopClass::Get(spec_class);
372 if (!IsWaypointClass(*cls)) return CMD_ERROR;
373 if (spec_index >= cls->GetSpecCount()) return CMD_ERROR;
374
375 const RoadStopSpec *roadstopspec = RoadStopClass::Get(spec_class)->GetSpec(spec_index);
376
377 /* The number of parts to build */
378 uint8_t count = axis == Axis::X ? height : width;
379
380 if ((axis == Axis::X ? width : height) != 1) return CMD_ERROR;
381 if (count == 0 || count > _settings_game.station.station_spread) return CMD_ERROR;
382
383 bool reuse = (station_to_join != NEW_STATION);
384 if (!reuse) station_to_join = StationID::Invalid();
385 bool distant_join = (station_to_join != StationID::Invalid());
386
387 if (distant_join && (!_settings_game.station.distant_join_stations || !Waypoint::IsValidID(station_to_join))) return CMD_ERROR;
388
389 TileArea roadstop_area(start_tile, width, height);
390 if (CommandCost ret = CheckStationSpread({}, roadstop_area); ret.Failed()) return ret;
391
392 /* Total road stop cost. */
393 Money unit_cost;
394 if (roadstopspec != nullptr) {
395 unit_cost = roadstopspec->GetBuildCost(Price::BuildStationTruck);
396 } else {
397 unit_cost = _price[Price::BuildStationTruck];
398 }
399 StationID est = StationID::Invalid();
400 CommandCost cost = CalculateRoadStopCost(roadstop_area, flags, true, StationType::RoadWaypoint, roadstopspec, axis, AxisToDiagDir(axis), &est, INVALID_ROADTYPE, unit_cost);
401 if (cost.Failed()) return cost;
402
403 Waypoint *wp = nullptr;
404 CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, roadstop_area, &wp, true);
405 if (ret.Failed()) return ret;
406
407 /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
408 TileIndex center_tile = start_tile + (count / 2) * TileOffsByAxis(OtherAxis(axis));
409 if (wp == nullptr && reuse) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT, _current_company, true);
410
411 if (wp != nullptr) {
412 /* Reuse an existing waypoint. */
413 if (!HasBit(wp->waypoint_flags, WPF_ROAD)) return CMD_ERROR;
414 if (wp->owner != _current_company) return CommandCost(STR_ERROR_TOO_CLOSE_TO_ANOTHER_WAYPOINT);
415
416 ret = CheckStationSpread(wp->spread, roadstop_area);
417 if (ret.Failed()) return ret;
418 } else {
419 /* Check if we can create a new waypoint. */
420 if (!Waypoint::CanAllocateItem()) return CommandCost(STR_ERROR_TOO_MANY_STATIONS_LOADING);
421 }
422
423 /* Check if we can allocate a custom spec to this waypoint. */
424 auto specindex = AllocateSpecToRoadStop(roadstopspec, wp);
425 if (!specindex.has_value()) return CommandCost(STR_ERROR_TOO_MANY_STATION_SPECS);
426
427 if (flags.Test(DoCommandFlag::Execute)) {
428 if (wp == nullptr) {
429 wp = Waypoint::Create(start_tile);
431 } else if (!wp->IsInUse()) {
432 /* Move existing (recently deleted) waypoint to the new location */
433 wp->xy = start_tile;
434 }
436
437 wp->spread.Add(roadstop_area);
438 if (specindex.has_value()) AssignSpecToRoadStop(roadstopspec, wp, *specindex);
439
440 if (roadstopspec != nullptr) {
441 /* Include this road stop spec's animation trigger bitmask
442 * in the station's cached copy. */
443 wp->cached_roadstop_anim_triggers.Set(roadstopspec->animation.triggers);
444 }
445
446 wp->delete_ctr = 0;
449 wp->string_id = STR_SV_STNAME_WAYPOINT;
450
451 if (wp->town == nullptr) MakeDefaultName(wp);
452
453 wp->UpdateVirtCoord();
454
455 /* Check every tile in the area. */
456 for (TileIndex cur_tile : roadstop_area) {
457 /* Get existing road types and owners before any tile clearing */
458 RoadType road_rt = MayHaveRoad(cur_tile) ? GetRoadType(cur_tile, RoadTramType::Road) : INVALID_ROADTYPE;
459 RoadType tram_rt = MayHaveRoad(cur_tile) ? GetRoadType(cur_tile, RoadTramType::Tram) : INVALID_ROADTYPE;
460 Owner road_owner = road_rt != INVALID_ROADTYPE ? GetRoadOwner(cur_tile, RoadTramType::Road) : _current_company;
461 Owner tram_owner = tram_rt != INVALID_ROADTYPE ? GetRoadOwner(cur_tile, RoadTramType::Tram) : _current_company;
462
463 if (IsRoadWaypointTile(cur_tile)) {
464 RemoveRoadWaypointStop(cur_tile, flags, *specindex);
465 }
466
467 wp->road_waypoint_area.Add(cur_tile);
468
469 /* Update company infrastructure counts. If the current tile is a normal road tile, remove the old
470 * bits first. */
471 if (IsNormalRoadTile(cur_tile)) {
472 UpdateCompanyRoadInfrastructure(road_rt, road_owner, -static_cast<int>(GetRoadBits(cur_tile, RoadTramType::Road).Count()));
473 UpdateCompanyRoadInfrastructure(tram_rt, tram_owner, -static_cast<int>(GetRoadBits(cur_tile, RoadTramType::Tram).Count()));
474 }
475
478
479 MakeDriveThroughRoadStop(cur_tile, wp->owner, road_owner, tram_owner, wp->index, StationType::RoadWaypoint, road_rt, tram_rt, axis);
480 SetCustomRoadStopSpecIndex(cur_tile, *specindex);
481 if (roadstopspec != nullptr) wp->SetRoadStopRandomBits(cur_tile, 0);
482
483 Company::Get(wp->owner)->infrastructure.station++;
484
485 MarkTileDirtyByTile(cur_tile);
486 }
488 }
489 return cost;
490}
491
499{
500 if (!HasTileWaterGround(tile)) return CommandCost(STR_ERROR_SITE_UNSUITABLE);
501 if (CommandCost ret = IsBuoyBridgeAboveOk(tile); ret.Failed()) return ret;
502
503 if (!IsTileFlat(tile)) return CommandCost(STR_ERROR_SITE_UNSUITABLE);
504
505 /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
506 Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_BUOY, OWNER_NONE, false);
507 if (wp == nullptr && !Waypoint::CanAllocateItem()) return CommandCost(STR_ERROR_TOO_MANY_STATIONS_LOADING);
508
510
511 /* Get the water class of the water tile before it is cleared.*/
512 WaterClass wc = GetWaterClass(tile);
513
514 if (!IsWaterTile(tile)) {
515 CommandCost ret = Command<Commands::LandscapeClear>::Do(flags | DoCommandFlag::Auto, tile);
516 if (ret.Failed()) return ret;
517 cost.AddCost(ret.GetCost());
518 }
519
520 if (flags.Test(DoCommandFlag::Execute)) {
521 if (wp == nullptr) {
522 wp = Waypoint::Create(tile);
523 } else {
524 /* Move existing (recently deleted) buoy to the new location */
525 wp->xy = tile;
526 InvalidateWindowData(WindowClass::WaypointView, wp->index);
527 }
528 wp->spread.Add(tile);
529
530 wp->string_id = STR_SV_STNAME_BUOY;
531
533 wp->owner = OWNER_NONE;
534
536
537 if (wp->town == nullptr) MakeDefaultName(wp);
538
539 MakeBuoy(tile, wp->index, wc);
543
544 wp->UpdateVirtCoord();
545 InvalidateWindowData(WindowClass::WaypointView, wp->index);
546 }
547
548 return cost;
549}
550
559{
560 /* XXX: strange stuff, allow clearing as invalid company when clearing landscape */
562
563 Waypoint *wp = Waypoint::GetByTile(tile);
564
565 if (HasStationInUse(wp->index, false, _current_company)) return CommandCost(STR_ERROR_BUOY_IS_IN_USE);
566 /* remove the buoy if there is a ship on tile when company goes bankrupt... */
567 if (!flags.Test(DoCommandFlag::Bankrupt)) {
569 if (ret.Failed()) return ret;
570 }
571
572 if (flags.Test(DoCommandFlag::Execute)) {
574
575 InvalidateWindowData(WindowClass::WaypointView, wp->index);
576
577 /* We have to set the water tile's state to the same state as before the
578 * buoy was placed. Otherwise one could plant a buoy on a canal edge,
579 * remove it and flood the land (if the canal edge is at level 0) */
580 MakeWaterKeepingClass(tile, GetTileOwner(tile));
581
582 wp->spread.Clear();
583
584 wp->UpdateVirtCoord();
585 wp->delete_ctr = 0;
586 }
587
589}
590
596static bool IsUniqueWaypointName(const std::string &name)
597{
598 for (const Waypoint *wp : Waypoint::Iterate()) {
599 if (!wp->name.empty() && wp->name == name) return false;
600 }
601
602 return true;
603}
604
612CommandCost CmdRenameWaypoint(DoCommandFlags flags, StationID waypoint_id, const std::string &text)
613{
614 Waypoint *wp = Waypoint::GetIfValid(waypoint_id);
615 if (wp == nullptr) return CMD_ERROR;
616
617 if (wp->owner != OWNER_NONE) {
619 if (ret.Failed()) return ret;
620 }
621
622 bool reset = text.empty();
623
624 if (!reset) {
626 if (!IsUniqueWaypointName(text)) return CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE);
627 }
628
629 if (flags.Test(DoCommandFlag::Execute)) {
630 if (reset) {
631 wp->name.clear();
632 } else {
633 wp->name = text;
634 }
635
636 wp->UpdateVirtCoord();
637 }
638 return CommandCost();
639}
640
648std::tuple<CommandCost, StationID> CmdMoveWaypointName(DoCommandFlags flags, StationID waypoint_id, TileIndex tile)
649{
650 Waypoint *wp = Waypoint::GetIfValid(waypoint_id);
651 if (wp == nullptr) return { CMD_ERROR, StationID::Invalid() };
652
653 if (wp->owner != OWNER_NONE) {
655 if (ret.Failed()) return { ret, StationID::Invalid() };
656 }
657
658 if (!wp->spread.Contains(tile)) {
659 return { CommandCost(STR_ERROR_SITE_UNSUITABLE), StationID::Invalid() };
660 }
661
662 bool other_station = false;
663 /* Check if the tile is the base tile of another station */
664 ForAllStationsRadius(tile, 0, [&](BaseStation *st) {
665 if (st != nullptr) {
666 if (st != wp && st->xy == tile) other_station = true;
667 }
668 });
669 if (other_station) return { CommandCost(STR_ERROR_SITE_UNSUITABLE), StationID::Invalid() };
670
671 if (flags.Test(DoCommandFlag::Execute)) {
672 wp->MoveSign(tile);
673
674 wp->UpdateVirtCoord();
675 }
676 return { CommandCost(), waypoint_id };
677}
678
684void CcMoveWaypointName(Commands, const CommandCost &result, StationID waypoint_id)
685{
686 if (result.Failed()) return;
687
689 Waypoint *wp = Waypoint::Get(waypoint_id);
691}
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Common return value for all commands.
bool Succeeded() const
Did this command succeed?
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Money GetCost() const
The costs as made up to this moment.
bool Failed() const
Did this command fail?
const Tspec * GetSpec(uint index) const
Get a spec from the class at a given index.
uint GetSpecCount() const
Get the number of allocated specs within the class.
static NewGRFClass * Get(StationClassID class_index)
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
void Add(TileIndex to_add)
Add a single tile to a tile area; enlarge if needed.
void Clear()
Clears the 'tile area', i.e.
const RailStationTileLayout & stl
Station tile layout being iterated.
uint position
Position within iterator.
StationGfx operator*() const
Dereference operator.
static Date date
Current date in days (day counter).
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Auto
don't allow building on structures
@ Execute
execute the given command
@ Bankrupt
company bankrupts, skip money check, skip vehicle on tile check in some cases
Commands
List of commands.
EnumBitSet< DoCommandFlag, uint16_t > DoCommandFlags
Bitset of DoCommandFlag elements.
Definition of stuff that is very close to a company, like the company struct itself.
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
void DirtyCompanyInfrastructureWindows(CompanyID company)
Redraw all windows with company infrastructure counts.
GUI Functions related to companies.
static constexpr Owner OWNER_NONE
The tile has no ownership.
bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
Axis OtherAxis(Axis a)
Select the other axis as provided.
Axis
Enumeration for the two axis X and Y.
@ X
The X axis.
@ Invalid
Flag for an invalid Axis.
@ Y
The y axis.
DiagDirection
Enumeration for diagonal directions.
Prices _price
Prices and also the fractional part.
Definition economy.cpp:109
static const uint ROAD_STOP_TRACKBIT_FACTOR
Multiplier for how many regular track bits a bay stop counts.
@ Construction
Construction costs.
@ BuildStationTruck
Price for building lorry stations.
@ BuildWaypointBuoy
Price for building new buoys.
@ ClearWaypointBuoy
Price for destroying buoys.
@ BuildWaypointRail
Price for building new rail waypoints.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Functions related to OTTD's landscape.
Point RemapCoords2(int x, int y)
Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap.
Definition landscape.h:97
Command definitions related to landscape (slopes etc.).
#define Point
Macro that prevents name conflicts between included headers.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:168
TileIndexDiff TileOffsByAxis(Axis axis)
Convert an Axis to a TileIndexDiff.
Definition map_func.h:559
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
@ Stations
Stations feature.
Definition newgrf.h:86
@ CBID_STATION_BUILD_TILE_LAYOUT
Called when building a station to customize the tile layout.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
void ErrorUnknownCallbackResult(GrfID grfid, uint16_t cbid, uint16_t cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
Functions/types related to NewGRF debugging.
void DeleteNewGRFInspectWindow(GrfSpecFeature feature, uint index)
Delete inspect window for a given feature and index.
std::optional< uint8_t > AllocateSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st)
Allocate a RoadStopSpec to a Station.
void AssignSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st, uint8_t specindex)
Assign a previously allocated RoadStopSpec specindex to a Station.
NewGRF definitions and structures for road stops.
bool IsWaypointClass(const RoadStopClass &cls)
Test if a RoadStopClass is the waypoint class.
PoolID< uint16_t, struct RoadStopClassIDTag, UINT16_MAX, UINT16_MAX > RoadStopClassID
Class IDs for stations.
void AssignSpecToStation(const StationSpec *spec, BaseStation *st, uint8_t specindex)
Assign a previously allocated StationSpec specindex to a Station.
void DeallocateSpecFromStation(BaseStation *st, uint8_t specindex)
Deallocate a StationSpec from a Station.
uint32_t GetPlatformInfo(StationGfx gfx, int platforms, int length, int platform, int position, bool centred)
Evaluate a tile's position within a station, and return the result in a bit-stuffed format.
std::optional< uint8_t > AllocateSpecToStation(const StationSpec *spec, BaseStation *st)
Allocate a StationSpec to a Station.
Header file for NewGRF stations.
NewGRFClass< StationSpec, StationClassID > StationClass
Class containing information relating to station classes.
PoolID< uint16_t, struct StationClassIDTag, UINT16_MAX, UINT16_MAX > StationClassID
Class IDs for stations.
static RailTileType GetRailTileType(Tile t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition rail_map.h:36
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
TrackBits GetTrackBits(Tile tile)
Gets the track bits of the given tile.
Definition rail_map.h:136
@ Normal
Normal rail tile without signals.
Definition rail_map.h:24
TrackBits GetRailReservationTrackBits(Tile t)
Returns the reserved track bits of the tile.
Definition rail_map.h:194
void UpdateCompanyRoadInfrastructure(RoadType rt, Owner o, int count)
Update road infrastructure counts for a company.
Definition road_cmd.cpp:184
bool MayHaveRoad(Tile t)
Test whether a tile can have road/tram types.
Definition road_map.cpp:21
RoadBits GetRoadBits(Tile t, RoadTramType rtt)
Get the present road bits for a specific road type.
Definition road_map.h:112
RoadBits GetAllRoadBits(Tile tile)
Get all set RoadBits on the given tile.
Definition road_map.h:125
RoadType GetRoadType(Tile t, RoadTramType rtt)
Get the road type for the given RoadTramType.
Definition road_map.h:175
static bool IsNormalRoadTile(Tile t)
Return whether a tile is a normal road tile.
Definition road_map.h:58
Owner GetRoadOwner(Tile t, RoadTramType rtt)
Get the owner of a specific road type.
Definition road_map.h:244
static constexpr RoadBits ROAD_X
Full road along the x-axis (south-west + north-east).
Definition road_type.h:67
EnumBitSet< RoadBit, uint8_t > RoadBits
Bitset of RoadBit elements.
Definition road_type.h:65
static constexpr RoadBits ROAD_Y
Full road along the y-axis (north-west + south-east).
Definition road_type.h:68
RoadType
The different roadtypes we support.
Definition road_type.h:24
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:29
@ Tram
Tram type.
Definition road_type.h:40
@ Road
Road type.
Definition road_type.h:39
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
static constexpr bool IsSteepSlope(Slope s)
Checks if a slope is steep.
Definition slope_func.h:36
Slope
Enumeration for the slope-type.
Definition slope_type.h:53
@ SLOPE_FLAT
a flat tile
Definition slope_type.h:54
CommandCost CheckStationSpread(TileArea area, const TileArea &new_area)
Check if adding a new area to a station will exceed the maximum station spread.
Definition station.cpp:526
CommandCost IsRailStationBridgeAboveOk(TileIndex tile, const StationSpec *spec, StationType type, StationGfx layout)
Test if a rail station can be built below a bridge.
CommandCost FindJoiningWaypoint(StationID existing_waypoint, StationID waypoint_to_join, bool adjacent, TileArea ta, Waypoint **wp, bool is_road)
Find a nearby waypoint that joins this waypoint.
CommandCost IsBuoyBridgeAboveOk(TileIndex tile)
Test if a buoy can be built below a bridge.
CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlags flags, int replacement_spec_index=-1)
Remove a road waypoint.
bool HasStationInUse(StationID station, bool include_company, CompanyID company)
Tests whether the company's vehicles have this station in orders.
void SetRailStationTileFlags(TileIndex tile, const StationSpec *statspec)
Set rail station tile flags for the given tile.
CommandCost CalculateRoadStopCost(TileArea tile_area, DoCommandFlags flags, bool is_drive_through, StationType station_type, const RoadStopSpec *roadstopspec, Axis axis, DiagDirection ddir, StationID *station, RoadType rt, Money unit_cost)
Calculates cost of new road stops within the area.
Functions related to stations.
@ Count
by amount of cargo
Declarations for accessing the k-d tree of stations.
void ForAllStationsRadius(TileIndex center, uint radius, Func func)
Call a function on all stations whose sign is within a radius of a center tile.
Functions related to station layouts.
void SetStationGfx(Tile t, StationGfx gfx)
Set the station graphics of this tile.
Definition station_map.h:78
void SetCustomStationSpecIndex(Tile t, uint8_t specindex)
Set the custom station spec for this tile.
bool IsRailWaypointTile(Tile t)
Is this tile a station tile and a rail waypoint?
void MakeDriveThroughRoadStop(Tile t, Owner station, Owner road, Owner tram, StationID sid, StationType rst, RoadType road_rt, RoadType tram_rt, Axis a)
Make the given tile a drivethrough roadstop tile.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:26
bool HasStationTileRail(Tile t)
Has this station tile a rail?
uint GetCustomStationSpecIndex(Tile t)
Get the custom station spec for this tile.
bool IsRailWaypoint(Tile t)
Is this station tile a rail waypoint?
void SetRailStationReservation(Tile t, bool b)
Set the reservation state of the rail station.
Axis GetRailStationAxis(Tile t)
Get the rail direction of a rail station.
void MakeRailWaypoint(Tile t, Owner o, StationID sid, Axis a, uint8_t section, RailType rt)
Make the given tile a rail waypoint tile.
bool IsRoadWaypointTile(Tile t)
Is this tile a station tile and a road waypoint?
uint8_t StationGfx
Index of station graphics.
Definition station_map.h:18
Axis GetDriveThroughStopAxis(Tile t)
Gets the axis of the drive through stop.
void MakeBuoy(Tile t, StationID sid, WaterClass wc)
Make the given tile a buoy tile.
bool HasStationReservation(Tile t)
Get the reservation state of the rail station.
void SetCustomRoadStopSpecIndex(Tile t, uint8_t specindex)
Set the custom road stop spec for this tile.
@ Dock
Station with a dock.
@ TruckStop
Station with truck stops.
@ Train
Station with train station.
@ BusStop
Station with bus stops.
StationType
Station types.
@ RailWaypoint
Waypoint for trains.
@ RoadWaypoint
Waypoint for trucks and busses.
static const uint MAX_LENGTH_STATION_NAME_CHARS
The maximum length of a station name in characters including '\0'.
Definition of base types and functions in a cross-platform compatible way.
size_t Utf8StringLength(std::string_view str)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition string.cpp:350
Functions related to low-level strings.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:423
Functions related to OTTD's strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
static constexpr StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames).
Base class for all station-ish types.
StringID string_id
Default name (town area) of station.
TileIndex xy
Base tile of the station.
virtual void MoveSign(TileIndex new_xy)
Move this station's main coordinate somewhere else.
StationFacilities facilities
The facilities that this station has.
TileArea train_station
Tile area the train 'station' part covers.
Owner owner
The owner of this station.
uint8_t delete_ctr
Delete counter. If greater than 0 then it is decremented until it reaches 0; the waypoint is then is ...
StationAnimationTriggers cached_roadstop_anim_triggers
NOSAVE: Combined animation trigger bitmask for road stops, used to determine if trigger processing sh...
bool IsInUse() const
Check whether the base station currently is in use; in use means that it is not scheduled for deletio...
Town * town
The town this station is associated with.
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
TrackedViewportSign sign
NOSAVE: Dimensions of sign.
TimerGameCalendar::Date build_date
Date of construction.
std::string name
Custom name.
TileArea spread
NOSAVE: Station spread tile area.
uint32_t station
Count of company owned station tiles.
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
T y
Y coordinate.
T x
X coordinate.
GrfID grfid
grfid that introduced this entity.
static Company * Get(auto index)
Road stop specification.
Money GetBuildCost(Price category) const
Get the cost for building a road stop of this type.
static Waypoint * Create(Targs &&... args)
static Pool::IterateWrapper< Waypoint > Iterate(size_t from=0)
static Waypoint * Get(auto index)
static Waypoint * GetIfValid(auto index)
Station specification.
CargoGRFFileProps grf_prop
Link to NewGRF.
Representation of a waypoint.
TileArea road_waypoint_area
Tile area the road waypoint part covers.
uint16_t waypoint_flags
Waypoint flags, see WaypointFlags.
void MoveSign(TileIndex new_xy) override
Move the waypoint main coordinate somewhere else.
void UpdateVirtCoord() override
Update the virtual coords needed to draw the waypoint sign.
bool IsTileFlat(TileIndex tile, int *h)
Check if a given tile is flat.
Definition tile_map.cpp:94
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
Slope GetTileSlope(TileIndex tile)
Return the slope of a given tile inside the map.
Definition tile_map.h:279
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
@ Station
A tile of a station or airport.
Definition tile_type.h:54
@ Railway
A tile with railway.
Definition tile_type.h:50
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.
Functions related to tile highlights.
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows).
Definition of the game-calendar-timer.
Base of the town class.
void MakeDefaultName(T *obj)
Set the default name for a depot/waypoint.
Definition town.h:335
Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track Axis::X -> Track::X Axis::Y -> Track::Y Uses the fact that...
Definition track_func.h:62
EnumBitSet< Track, uint8_t > TrackBits
Bitset of Track elements.
Definition track_type.h:43
@ X
Track along the x-axis (north-east to south-west).
Definition track_type.h:21
@ Y
Track along the y-axis (north-west to south-east).
Definition track_type.h:22
CommandCost EnsureNoVehicleOnGround(TileIndex tile)
Ensure there is no vehicle at the ground at the given position.
Definition vehicle.cpp:559
Functions related to vehicles.
void SetViewportCatchmentWaypoint(const Waypoint *wp, bool sel)
Select or deselect waypoint for coverage area highlight.
Functions related to (drawing on) viewports.
Declarations for accessing the k-d tree of viewports.
Functions related to water management.
void ClearNeighbourNonFloodingStates(TileIndex tile)
Clear non-flooding state of the tiles around a tile.
void CheckForDockingTile(TileIndex t)
Mark the supplied tile as a docking tile if it is suitable for docking.
bool HasTileWaterGround(Tile t)
Checks whether the tile has water at the ground.
Definition water_map.h:356
WaterClass
classes of water (for WaterTileType::Clear water tile type).
Definition water_map.h:41
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:116
bool IsWaterTile(Tile t)
Is it a water tile with plain water?
Definition water_map.h:194
Base of waypoints.
@ WPF_ROAD
This is a road waypoint.
static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID *waypoint)
Check whether the given tile is suitable for a waypoint.
CommandCost CmdBuildRoadWaypoint(DoCommandFlags flags, TileIndex start_tile, Axis axis, uint8_t width, uint8_t height, RoadStopClassID spec_class, uint16_t spec_index, StationID station_to_join, bool adjacent)
Build a road waypoint on an existing road.
CommandCost CmdBuildRailWaypoint(DoCommandFlags flags, TileIndex start_tile, Axis axis, uint8_t width, uint8_t height, StationClassID spec_class, uint16_t spec_index, StationID station_to_join, bool adjacent)
Convert existing rail to waypoint.
void CcMoveWaypointName(Commands, const CommandCost &result, StationID waypoint_id)
Callback function that is called after a name is moved.
CommandCost IsRailStationBridgeAboveOk(TileIndex tile, const StationSpec *spec, StationType type, StationGfx layout)
Test if a rail station can be built below a bridge.
std::tuple< CommandCost, StationID > CmdMoveWaypointName(DoCommandFlags flags, StationID waypoint_id, TileIndex tile)
Move a waypoint name.
Axis GetAxisForNewRailWaypoint(TileIndex tile)
Get the axis for a new rail waypoint.
Axis GetAxisForNewRoadWaypoint(TileIndex tile)
Get the axis for a new road waypoint.
CommandCost FindJoiningWaypoint(StationID existing_station, StationID station_to_join, bool adjacent, TileArea ta, Waypoint **wp, bool is_road)
Find a nearby waypoint that joins this waypoint.
CommandCost IsBuoyBridgeAboveOk(TileIndex tile)
Test if a buoy can be built below a bridge.
CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlags flags, int replacement_spec_index)
Remove a road waypoint.
CommandCost RemoveBuoy(TileIndex tile, DoCommandFlags flags)
Remove a buoy.
CommandCost ClearTile_Station(TileIndex tile, DoCommandFlags flags)
Tile callback function signature for clearing a tile.
CommandCost CmdRenameWaypoint(DoCommandFlags flags, StationID waypoint_id, const std::string &text)
Rename a waypoint.
CommandCost CmdBuildBuoy(DoCommandFlags flags, TileIndex tile)
Build a buoy.
static bool IsUniqueWaypointName(const std::string &name)
Check whether the name is unique amongst the waypoints.
static Waypoint * FindDeletedWaypointCloseTo(TileIndex tile, StringID str, CompanyID cid, bool is_road)
Find a deleted waypoint close to a tile.
CommandCost CalculateRoadStopCost(TileArea tile_area, DoCommandFlags flags, bool is_drive_through, StationType station_type, const RoadStopSpec *roadstopspec, Axis axis, DiagDirection ddir, StationID *est, RoadType rt, Money unit_cost)
Calculates cost of new road stops within the area.
Command definitions related to waypoints.
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3377
Window functions not directly related to making/drawing windows.
Entry point for OpenTTD to YAPF's cache.
void YapfNotifyTrackLayoutChange(TileIndex tile, Track track)
Use this function to notify YAPF that track layout (or signal configuration) has change.