OpenTTD Source 20261112-master-gbcbec5e63f
newgrf_roadstop.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#include "debug.h"
12#include "station_base.h"
13#include "station_func.h"
14#include "newgrf_badge.h"
15#include "newgrf_roadstop.h"
16#include "newgrf_cargo.h"
17#include "newgrf_roadtype.h"
18#include "gfx_type.h"
19#include "company_func.h"
20#include "road.h"
22#include "town.h"
23#include "tile_cmd.h"
24#include "viewport_func.h"
26
27#include "table/strings.h"
28
29#include "newgrf_class_func.h"
30
31#include "safeguards.h"
32
33template <>
42
43template <>
45{
46 return true;
47}
48
49/* Instantiate RoadStopClass. */
51
52static const uint NUM_ROADSTOPSPECS_PER_STATION = 63;
53
55{
56 if (this->st == nullptr) return 0;
57
58 uint32_t bits = this->st->random_bits;
59 if (this->tile != INVALID_TILE && Station::IsExpected(this->st)) {
60 bits |= Station::From(this->st)->GetRoadStopRandomBits(this->tile) << 16;
61 }
62 return bits;
63}
64
66{
67 if (this->st == nullptr) return 0;
68
69 StationRandomTriggers triggers = st->waiting_random_triggers;
70
71 auto it = this->st->tile_waiting_random_triggers.find(this->tile);
72 if (it != std::end(this->st->tile_waiting_random_triggers)) triggers.Set(it->second);
73
74 return triggers.base();
75}
76
77uint32_t RoadStopScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
78{
79 auto get_road_type_variable = [&](RoadTramType rtt) -> uint32_t {
80 RoadType rt;
81 if (this->tile == INVALID_TILE) {
82 rt = (GetRoadTramType(this->roadtype) == rtt) ? this->roadtype : INVALID_ROADTYPE;
83 } else {
84 rt = GetRoadType(this->tile, rtt);
85 }
86 if (rt == INVALID_ROADTYPE) {
87 return 0xFFFFFFFF;
88 } else {
89 return GetReverseRoadTypeTranslation(rt, this->roadstopspec->grf_prop.grffile);
90 }
91 };
92
93 switch (variable) {
94 /* View/rotation */
95 case 0x40: return this->view;
96
97 /* Stop type: 0: bus, 1: truck, 2: waypoint */
98 case 0x41:
99 if (this->type == StationType::Bus) return 0;
100 if (this->type == StationType::Truck) return 1;
101 return 2;
102
103 /* Terrain type */
104 case 0x42: return this->tile == INVALID_TILE ? 0 : (GetTileSlope(this->tile) << 8 | GetTerrainType(this->tile, TileContext::Normal));
105
106 /* Road type */
107 case 0x43: return get_road_type_variable(RoadTramType::Road);
108
109 /* Tram type */
110 case 0x44: return get_road_type_variable(RoadTramType::Tram);
111
112 /* Town zone and Manhattan distance of closest town */
113 case 0x45: {
114 if (this->tile == INVALID_TILE) return to_underlying(HouseZone::TownEdge) << 16;
115 const Town *t = (this->st == nullptr) ? ClosestTownFromTile(this->tile, UINT_MAX) : this->st->town;
116 return t != nullptr ? (to_underlying(GetTownRadiusGroup(t, this->tile)) << 16 | ClampTo<uint16_t>(DistanceManhattan(this->tile, t->xy))) : to_underlying(HouseZone::TownEdge) << 16;
117 }
118
119 /* Get square of Euclidean distance of closest town */
120 case 0x46: {
121 if (this->tile == INVALID_TILE) return 0;
122 const Town *t = (this->st == nullptr) ? ClosestTownFromTile(this->tile, UINT_MAX) : this->st->town;
123 return t != nullptr ? DistanceSquare(this->tile, t->xy) : 0;
124 }
125
126 /* Company information */
127 case 0x47: return GetCompanyInfo(this->st == nullptr ? _current_company : this->st->owner);
128
129 /* Animation frame */
130 case 0x49: return this->tile == INVALID_TILE ? 0 : this->st->GetRoadStopAnimationFrame(this->tile);
131
132 /* Misc info */
133 case 0x50: {
134 uint32_t result = 0;
135 if (this->tile == INVALID_TILE) {
136 SetBit(result, 4);
137 }
138 return result;
139 }
140
141 /* Variables which use the parameter */
142 /* Variables 0x60 to 0x65 and 0x69 are handled separately below */
143
144 /* Animation frame of nearby tile */
145 case 0x66: {
146 if (this->tile == INVALID_TILE) return UINT_MAX;
147 TileIndex tile = this->tile;
148 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
149 return (IsAnyRoadStopTile(tile) && GetStationIndex(tile) == this->st->index) ? this->st->GetRoadStopAnimationFrame(tile) : UINT_MAX;
150 }
151
152 /* Land info of nearby tile */
153 case 0x67: {
154 if (this->tile == INVALID_TILE) return 0;
155 TileIndex tile = this->tile;
156 if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
157 return GetNearbyTileInformation(tile, this->ro.grffile->grf_version >= 8);
158 }
159
160 /* Road stop info of nearby tiles */
161 case 0x68: {
162 if (this->tile == INVALID_TILE) return 0xFFFFFFFF;
163 TileIndex nearby_tile = GetNearbyTile(parameter, this->tile);
164
165 if (!IsAnyRoadStopTile(nearby_tile)) return 0xFFFFFFFF;
166
167 GrfID grfid = this->st->roadstop_speclist[GetCustomRoadStopSpecIndex(this->tile)].grfid;
168 bool same_orientation = GetStationGfx(this->tile) == GetStationGfx(nearby_tile);
169 bool same_station = GetStationIndex(nearby_tile) == this->st->index;
170 uint32_t res = GetStationGfx(nearby_tile) << 12 | !same_orientation << 11 | !!same_station << 10;
171 StationType type = GetStationType(nearby_tile);
172 if (type == StationType::Truck) res |= (1 << 16);
173 if (type == StationType::RoadWaypoint) res |= (2 << 16);
174 if (type == this->type) SetBit(res, 20);
175
176 if (IsCustomRoadStopSpecIndex(nearby_tile)) {
177 const auto &sm = BaseStation::GetByTile(nearby_tile)->roadstop_speclist[GetCustomRoadStopSpecIndex(nearby_tile)];
178 res |= 1 << (sm.grfid != grfid ? 9 : 8) | ClampTo<uint8_t>(sm.localidx);
179 }
180 return res;
181 }
182
183 /* GRFID of nearby road stop tiles */
184 case 0x6A: {
185 if (this->tile == INVALID_TILE) return 0xFFFFFFFF;
186 TileIndex nearby_tile = GetNearbyTile(parameter, this->tile);
187
188 if (!IsAnyRoadStopTile(nearby_tile)) return 0xFFFFFFFF;
189 if (!IsCustomRoadStopSpecIndex(nearby_tile)) return 0;
190
191 const auto &sm = BaseStation::GetByTile(nearby_tile)->roadstop_speclist[GetCustomRoadStopSpecIndex(nearby_tile)];
192 return FlattenNewGRFLabel(sm.grfid);
193 }
194
195 /* 16 bit road stop ID of nearby tiles */
196 case 0x6B: {
197 if (this->tile == INVALID_TILE) return 0xFFFFFFFF;
198 TileIndex nearby_tile = GetNearbyTile(parameter, this->tile);
199
200 if (!IsAnyRoadStopTile(nearby_tile)) return 0xFFFFFFFF;
201 if (!IsCustomRoadStopSpecIndex(nearby_tile)) return 0xFFFE;
202
203 GrfID grfid = this->st->roadstop_speclist[GetCustomRoadStopSpecIndex(this->tile)].grfid;
204
205 const auto &sm = BaseStation::GetByTile(nearby_tile)->roadstop_speclist[GetCustomRoadStopSpecIndex(nearby_tile)];
206 if (sm.grfid == grfid) {
207 return sm.localidx;
208 }
209
210 return 0xFFFE;
211 }
212
213 case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, this->roadstopspec->badges, parameter);
214
215 case 0xF0: return this->st == nullptr ? 0 : this->st->facilities.base(); // facilities
216
217 case 0xFA: return ClampTo<uint16_t>((this->st == nullptr ? TimerGameCalendar::date : this->st->build_date) - CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR); // build date
218 }
219
220 if (this->st != nullptr) return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
221
222 available = false;
223 return UINT_MAX;
224}
225
226RoadStopResolverObject::RoadStopResolverObject(const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view,
227 CallbackID callback, uint32_t param1, uint32_t param2)
228 : SpecializedResolverObject<StationRandomTriggers>(roadstopspec->grf_prop.grffile, callback, param1, param2), roadstop_scope(*this, st, roadstopspec, tile, roadtype, type, view)
229{
231
232 if (st == nullptr) {
233 /* No station, so we are in a purchase list */
235 } else if (Station::IsExpected(st)) {
236 const Station *station = Station::From(st);
237 /* Pick the first cargo that we have waiting */
238 for (const auto &[cargo, spritegroup] : roadstopspec->grf_prop.spritegroups) {
239 if (cargo < NUM_CARGO && station->goods[cargo].TotalCount() > 0) {
240 ctype = static_cast<CargoType>(cargo);
241 this->root_spritegroup = spritegroup;
242 break;
243 }
244 }
245 }
246
247 this->root_spritegroup = this->roadstop_scope.roadstopspec->grf_prop.GetSpriteGroup(ctype);
248 if (this->root_spritegroup == nullptr) {
250 this->root_spritegroup = this->roadstop_scope.roadstopspec->grf_prop.GetSpriteGroup(ctype);
251 }
252
253 /* Remember the cargo type we've picked */
254 this->roadstop_scope.cargo_type = ctype;
255}
256
257TownScopeResolver *RoadStopResolverObject::GetTown()
258{
259 if (!this->town_scope.has_value()) {
260 Town *t;
261 if (this->roadstop_scope.st != nullptr) {
262 t = this->roadstop_scope.st->town;
263 } else {
264 t = ClosestTownFromTile(this->roadstop_scope.tile, UINT_MAX);
265 }
266 if (t == nullptr) return nullptr;
267 this->town_scope.emplace(*this, t, this->roadstop_scope.st == nullptr);
268 }
269 return &*this->town_scope;
270}
271
272uint16_t GetRoadStopCallback(CallbackID callback, uint32_t param1, uint32_t param2, const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view, std::span<int32_t> regs100)
273{
274 RoadStopResolverObject object(roadstopspec, st, tile, roadtype, type, view, callback, param1, param2);
275 return object.ResolveCallback(regs100);
276}
277
287void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view)
288{
289 assert(roadtype != INVALID_ROADTYPE);
290 assert(spec != nullptr);
291
292 const RoadTypeInfo *rti = GetRoadTypeInfo(roadtype);
293 RoadStopResolverObject object(spec, nullptr, INVALID_TILE, roadtype, type, view);
294 const auto *group = object.Resolve<TileLayoutSpriteGroup>();
295 if (group == nullptr) return;
296 auto processor = group->ProcessRegisters(object, nullptr);
297 auto dts = processor.GetLayout();
298
300
301 SpriteID image = dts.ground.sprite;
302 PaletteID pal = dts.ground.pal;
303
304 RoadStopDrawModes draw_mode;
305 if (spec->flags.Test(RoadStopSpecFlag::DrawModeRegister)) {
306 draw_mode = static_cast<RoadStopDrawModes>(object.GetRegister(0x100));
307 } else {
308 draw_mode = spec->draw_mode;
309 }
310
311 if (type == StationType::RoadWaypoint) {
312 DrawSprite(SPR_ROAD_PAVED_STRAIGHT_X, PAL_NONE, x, y);
313 if (draw_mode.Test(RoadStopDrawMode::WaypGround) && GB(image, 0, SPRITE_WIDTH) != 0) {
314 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
315 }
316 } else if (GB(image, 0, SPRITE_WIDTH) != 0) {
317 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
318 }
319
320 if (view >= 4) {
321 /* Drive-through stop */
322 uint sprite_offset = 5 - view;
323
324 /* Road underlay takes precedence over tram */
325 if (type == StationType::RoadWaypoint || draw_mode.Test(RoadStopDrawMode::Overlay)) {
326 if (rti->UsesOverlay()) {
328 DrawSprite(ground + sprite_offset, PAL_NONE, x, y);
329
331 if (overlay) DrawSprite(overlay + sprite_offset, PAL_NONE, x, y);
332 } else if (RoadTypeIsTram(roadtype)) {
333 DrawSprite(SPR_TRAMWAY_TRAM + sprite_offset, PAL_NONE, x, y);
334 }
335 }
336 } else {
337 /* Bay stop */
338 if (draw_mode.Test(RoadStopDrawMode::Road) && rti->UsesOverlay()) {
340 DrawSprite(ground + view, PAL_NONE, x, y);
341 }
342 }
343
344 DrawCommonTileSeqInGUI(x, y, &dts, 0, 0, palette, true);
345}
346
347std::optional<SpriteLayoutProcessor> GetRoadStopLayout(TileInfo *ti, const RoadStopSpec *spec, BaseStation *st, StationType type, int view, std::span<int32_t> regs100)
348{
349 RoadStopResolverObject object(spec, st, ti->tile, INVALID_ROADTYPE, type, view);
350 auto group = object.Resolve<TileLayoutSpriteGroup>();
351 if (group == nullptr) return std::nullopt;
352 for (uint i = 0; i < regs100.size(); ++i) {
353 regs100[i] = object.GetRegister(0x100 + i);
354 }
355 return group->ProcessRegisters(object, nullptr);
356}
357
368uint16_t GetAnimRoadStopCallback(CallbackID callback, uint32_t param1, uint32_t param2, const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, int)
369{
370 return GetRoadStopCallback(callback, param1, param2, roadstopspec, st, tile, INVALID_ROADTYPE, GetStationType(tile), GetStationGfx(tile));
371}
372
374 static uint8_t Get(BaseStation *st, TileIndex tile) { return st->GetRoadStopAnimationFrame(tile); }
375 static bool Set(BaseStation *st, TileIndex tile, uint8_t frame) { return st->SetRoadStopAnimationFrame(tile, frame); }
376};
377
386
387void AnimateRoadStopTile(TileIndex tile)
388{
389 const RoadStopSpec *ss = GetRoadStopSpec(tile);
390 if (ss == nullptr) return;
391
393}
394
395void TriggerRoadStopAnimation(BaseStation *st, TileIndex trigger_tile, StationAnimationTrigger trigger, CargoType cargo_type)
396{
397 assert(st != nullptr);
398
399 /* Check the cached animation trigger bitmask to see if we need
400 * to bother with any further processing. */
401 if (!st->cached_roadstop_anim_triggers.Test(trigger)) return;
402
403 uint16_t random_bits = Random();
404 auto process_tile = [&](TileIndex cur_tile) {
405 const RoadStopSpec *ss = GetRoadStopSpec(cur_tile);
406 if (ss != nullptr && ss->animation.triggers.Test(trigger)) {
407 uint8_t var18_extra = 0;
408 if (IsValidCargoType(cargo_type)) {
409 var18_extra |= ss->grf_prop.grffile->cargo_map[cargo_type] << 8;
410 }
411 RoadStopAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIMATION_TRIGGER, ss, st, cur_tile, (random_bits << 16) | GB(Random(), 0, 16), to_underlying(trigger) | var18_extra);
412 }
413 };
414
416 for (const RoadStopTileData &tile_data : st->custom_roadstop_tile_data) {
417 process_tile(tile_data.tile);
418 }
419 } else {
420 process_tile(trigger_tile);
421 }
422}
423
433{
434 /* List of coverage areas for each animation trigger */
437 };
438
439 assert(st != nullptr);
440
441 /* Check the cached cargo trigger bitmask to see if we need
442 * to bother with any further processing.
443 * Note: cached_roadstop_cargo_triggers must be non-zero even for cargo-independent triggers. */
444 if (st->cached_roadstop_cargo_triggers.None()) return;
445 if (IsValidCargoType(cargo_type) && !st->cached_roadstop_cargo_triggers.Test(cargo_type)) return;
446
447 TriggerArea ta = tas[trigger];
448 if (ta == TriggerArea::Whole) st->waiting_random_triggers.Set(trigger);
449 StationRandomTriggers used_random_triggers;
450
451 uint32_t whole_reseed = 0;
452
453 CargoTypes cargo_waiting{};
454 if (trigger == StationRandomTrigger::CargoTaken) {
455 cargo_waiting = GetCargoWaitingMask(Station::From(st));
456 }
457
458 auto process_tile = [&](TileIndex cur_tile) {
459 const RoadStopSpec *ss = GetRoadStopSpec(cur_tile);
460 if (ss == nullptr) return;
461
462 st->tile_waiting_random_triggers[tile].Set(trigger);
463
464 /* Cargo taken "will only be triggered if all of those
465 * cargo types have no more cargo waiting." */
466 if (trigger == StationRandomTrigger::CargoTaken) {
467 if (ss->cargo_triggers.Any(cargo_waiting)) return;
468 }
469
470 if (!IsValidCargoType(cargo_type) || ss->cargo_triggers.Test(cargo_type)) {
471 RoadStopResolverObject object(ss, st, cur_tile, INVALID_ROADTYPE, GetStationType(cur_tile), GetStationGfx(cur_tile));
472 object.SetWaitingRandomTriggers(st->waiting_random_triggers | st->tile_waiting_random_triggers[tile]);
473
474 object.ResolveRerandomisation();
475
476 st->tile_waiting_random_triggers[tile].Reset(object.GetUsedRandomTriggers());
477 used_random_triggers.Set(object.GetUsedRandomTriggers());
478
479 uint32_t reseed = object.GetReseedSum();
480 if (reseed != 0) {
481 whole_reseed |= reseed;
482 reseed >>= 16;
483
484 /* Set individual tile random bits */
485 uint8_t random_bits = st->GetRoadStopRandomBits(cur_tile);
486 random_bits &= ~reseed;
487 random_bits |= Random() & reseed;
488 st->SetRoadStopRandomBits(cur_tile, random_bits);
489
490 MarkTileDirtyByTile(cur_tile);
491 }
492 }
493 };
494 if (ta == TriggerArea::Whole) {
495 for (const RoadStopTileData &tile_data : st->custom_roadstop_tile_data) {
496 process_tile(tile_data.tile);
497 }
498 } else {
499 process_tile(tile);
500 }
501
502 /* Update whole station random bits */
503 st->waiting_random_triggers.Reset(used_random_triggers);
504 if ((whole_reseed & 0xFFFF) != 0) {
505 st->random_bits &= ~whole_reseed;
506 st->random_bits |= Random() & whole_reseed;
507 }
508}
509
517{
518 for (const auto &cls : RoadStopClass::Classes()) {
519 /* Ignore the waypoint class. */
520 if (IsWaypointClass(cls)) continue;
521 /* Ignore the default class with only the default station. */
522 if (cls.Index() == ROADSTOP_CLASS_DFLT && cls.GetSpecCount() == 1) continue;
523 if (GetIfClassHasNewStopsByType(&cls, rs, roadtype)) return true;
524 }
525 return false;
526}
527
535bool GetIfClassHasNewStopsByType(const RoadStopClass *roadstopclass, RoadStopType rs, RoadType roadtype)
536{
537 for (const auto spec : roadstopclass->Specs()) {
538 if (GetIfStopIsForType(spec, rs, roadtype)) return true;
539 }
540 return false;
541}
542
550bool GetIfStopIsForType(const RoadStopSpec *roadstopspec, RoadStopType rs, RoadType roadtype)
551{
552 /* The roadstopspec is nullptr, must be the default station, always return true. */
553 if (roadstopspec == nullptr) return true;
554
555 if (roadstopspec->flags.Test(RoadStopSpecFlag::RoadOnly) && !RoadTypeIsRoad(roadtype)) return false;
556 if (roadstopspec->flags.Test(RoadStopSpecFlag::TramOnly) && !RoadTypeIsTram(roadtype)) return false;
557
558 if (roadstopspec->stop_type == ROADSTOPTYPE_ALL) return true;
559
560 switch (rs) {
562 if (roadstopspec->stop_type == ROADSTOPTYPE_PASSENGER) return true;
563 break;
564
566 if (roadstopspec->stop_type == ROADSTOPTYPE_FREIGHT) return true;
567 break;
568
569 default:
570 NOT_REACHED();
571 }
572 return false;
573}
574
575const RoadStopSpec *GetRoadStopSpec(TileIndex t)
576{
577 if (!IsCustomRoadStopSpecIndex(t)) return nullptr;
578
579 const BaseStation *st = BaseStation::GetByTile(t);
580 uint specindex = GetCustomRoadStopSpecIndex(t);
581 return specindex < st->roadstop_speclist.size() ? st->roadstop_speclist[specindex].spec : nullptr;
582}
583
590std::optional<uint8_t> AllocateSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st)
591{
592 uint i;
593
594 if (spec == nullptr) return 0;
595
596 /* If station doesn't exist yet then the first slot is available. */
597 if (st == nullptr) return 1;
598
599 /* Try to find the same spec and return that one */
600 for (i = 1; i < st->roadstop_speclist.size() && i < NUM_ROADSTOPSPECS_PER_STATION; i++) {
601 if (st->roadstop_speclist[i].spec == spec) return i;
602 }
603
604 /* Try to find an unused spec slot */
605 for (i = 1; i < st->roadstop_speclist.size() && i < NUM_ROADSTOPSPECS_PER_STATION; i++) {
606 if (st->roadstop_speclist[i].spec == nullptr && st->roadstop_speclist[i].grfid.Empty()) break;
607 }
608
610 /* Full, give up */
611 return std::nullopt;
612 }
613
614 return i;
615}
616
623void AssignSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st, uint8_t specindex)
624{
625 if (specindex == 0) return;
626 if (specindex >= st->roadstop_speclist.size()) st->roadstop_speclist.resize(specindex + 1);
627
628 st->roadstop_speclist[specindex].spec = spec;
629 st->roadstop_speclist[specindex].grfid = spec->grf_prop.grfid;
630 st->roadstop_speclist[specindex].localidx = spec->grf_prop.local_id;
631
633}
634
640void DeallocateSpecFromRoadStop(BaseStation *st, uint8_t specindex)
641{
642 /* specindex of 0 (default) is never freeable */
643 if (specindex == 0) return;
644
645 /* Check custom road stop tiles if the specindex is still in use */
646 for (const RoadStopTileData &tile_data : st->custom_roadstop_tile_data) {
647 if (GetCustomRoadStopSpecIndex(tile_data.tile) == specindex) {
648 return;
649 }
650 }
651
652 /* This specindex is no longer in use, so deallocate it */
653 st->roadstop_speclist[specindex].spec = nullptr;
654 st->roadstop_speclist[specindex].grfid = {};
655 st->roadstop_speclist[specindex].localidx = 0;
656
657 /* If this was the highest spec index, reallocate */
658 if (specindex == st->roadstop_speclist.size() - 1) {
659 size_t num_specs;
660 for (num_specs = st->roadstop_speclist.size() - 1; num_specs > 0; num_specs--) {
661 if (!st->roadstop_speclist[num_specs].grfid.Empty()) break;
662 }
663
664 if (num_specs > 0) {
665 st->roadstop_speclist.resize(num_specs + 1);
666 } else {
667 st->roadstop_speclist.clear();
670 return;
671 }
672 }
673
675}
676
682{
685
686 /* Combine animation trigger bitmask for all road stop specs
687 * of this station. */
688 for (const auto &sm : GetStationSpecList<RoadStopSpec>(st)) {
689 if (sm.spec == nullptr) continue;
690 st->cached_roadstop_anim_triggers.Set(sm.spec->animation.triggers);
691 st->cached_roadstop_cargo_triggers.Set(sm.spec->cargo_triggers);
692 }
693}
std::vector< SpecMapping< T > > & GetStationSpecList(BaseStation *bst)
Get spec mapping list for each supported custom spec type.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:112
EnumBitSet< CargoType, uint64_t > CargoTypes
Bitset of CargoType elements.
Definition cargo_type.h:115
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
constexpr bool None() const
Test if none of the values are 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.
Struct containing information relating to NewGRF classes for stations and airports.
std::span< Tspec *const > Specs() const
Get read-only span of specs of this class.
void Insert(Tspec *spec)
Insert a spec into the class, and update its index.
static std::span< NewGRFClass< RoadStopSpec, RoadStopClassID > const > Classes()
static NewGRFClass * Get(RoadStopClassID class_index)
StringID name
Name of this class.
static RoadStopClassID Allocate(GlobalID global_id)
DrawTileSpriteSpan GetLayout() const
Returns the result spritelayout after preprocessing.
static Date date
Current date in days (day counter).
static constexpr TimerGame< struct Calendar >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
PaletteID GetCompanyPalette(CompanyID company)
Get the palette for recolouring with a company colour.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
Functions related to debugging.
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
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition gfx.cpp:1032
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
@ Town
Destination is a town.
Definition goal_type.h:55
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
@ TownEdge
Edge of the town; roads without pavement.
Definition house.h:60
uint DistanceSquare(TileIndex t0, TileIndex t1)
Gets the 'Square' distance between the two given tiles.
Definition map.cpp:185
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:168
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
constexpr uint32_t FlattenNewGRFLabel(T label)
Flatten a NewGRF related label to a 32 bits integer.
Definition newgrf.h:262
Function implementations related to NewGRF animation.
uint32_t GetBadgeVariableResult(const GRFFile &grffile, std::span< const BadgeID > badges, uint32_t parameter)
Test for a matching badge in a list of badges, returning the number of matching bits.
Functions related to NewGRF badges.
CallbackID
List of implemented NewGRF callbacks.
@ CBID_STATION_ANIMATION_TRIGGER
Called for periodically starting or stopping the animation.
@ CBID_STATION_ANIMATION_NEXT_FRAME
Called to determine station tile next animation frame.
@ CBID_STATION_ANIMATION_SPEED
Called to indicate how long the current animation frame should last.
RoadStopCallbackMask
Callback masks for road stops.
@ AnimationNextFrame
Use a custom next frame callback.
@ AnimationSpeed
Customize the animation speed of the road stop.
Cargo support for NewGRFs.
Implementation of the NewGRF class' functions.
uint32_t GetCompanyInfo(CompanyID owner, const Livery *l)
Returns company information like in vehicle var 43 or station var 43.
uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8)
Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62,...
uint32_t GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
TileIndex GetNearbyTile(uint8_t parameter, TileIndex tile, bool signed_offsets, Axis axis)
Get the tile at the given offset.
@ Normal
Nothing special.
static const uint NUM_ROADSTOPSPECS_PER_STATION
Maximum number of parts per station.
void TriggerRoadStopRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type)
Trigger road stop randomisation.
void DeallocateSpecFromRoadStop(BaseStation *st, uint8_t specindex)
Deallocate a RoadStopSpec from a Station.
std::optional< uint8_t > AllocateSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st)
Allocate a RoadStopSpec to a Station.
void RoadStopUpdateCachedTriggers(BaseStation *st)
Update the cached animation trigger bitmask for a station.
void AssignSpecToRoadStop(const RoadStopSpec *spec, BaseStation *st, uint8_t specindex)
Assign a previously allocated RoadStopSpec specindex to a Station.
bool GetIfClassHasNewStopsByType(const RoadStopClass *roadstopclass, RoadStopType rs, RoadType roadtype)
Checks if the given RoadStopClass has any specs assigned to it, compatible with the given RoadStopTyp...
void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view)
Draw representation of a road stop tile for GUI purposes.
bool GetIfNewStopsByType(RoadStopType rs, RoadType roadtype)
Checks if there's any new stations by a specific RoadStopType.
uint16_t GetAnimRoadStopCallback(CallbackID callback, uint32_t param1, uint32_t param2, const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, int)
Perform the road stop callback in the context of the AnimationBase callback.
bool GetIfStopIsForType(const RoadStopSpec *roadstopspec, RoadStopType rs, RoadType roadtype)
Checks if the given RoadStopSpec is compatible with the given RoadStopType.
NewGRF definitions and structures for road stops.
@ Overlay
Drive-through stops: Draw the road overlay, e.g. pavement.
@ WaypGround
Waypoints: Draw the sprite layout ground tile (on top of the road).
@ Road
Bay stops: Draw the road itself.
bool IsWaypointClass(const RoadStopClass &cls)
Test if a RoadStopClass is the waypoint class.
static constexpr RoadStopClassID ROADSTOP_CLASS_DFLT
Default road stop class.
EnumBitSet< RoadStopDrawMode, uint8_t > RoadStopDrawModes
Bitset of RoadStopDrawMode elements.
static const RoadStopClass::GlobalID ROADSTOP_CLASS_LABEL_WAYPOINT
Class label for default road waypoints.
static const RoadStopClass::GlobalID ROADSTOP_CLASS_LABEL_DEFAULT
Class label for default road stops.
@ ROADSTOPTYPE_FREIGHT
This RoadStop is for freight (truck) stops.
@ ROADSTOPTYPE_ALL
This RoadStop is for both types of station road stops.
@ ROADSTOPTYPE_PASSENGER
This RoadStop is for passenger (bus) stops.
@ RoadOnly
Only show in the road build menu (not tram).
@ Cb141RandomBits
Callback 141 needs random bits.
@ TramOnly
Only show in the tram build menu (not road).
@ DrawModeRegister
Read draw mode from register 0x100.
uint8_t GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile)
Perform a reverse roadtype lookup to get the GRF internal ID.
SpriteID GetCustomRoadSprite(const RoadTypeInfo *rti, TileIndex tile, RoadSpriteType rtsg, TileContext context, uint *num_results)
Get the sprite to draw for the given tile.
NewGRF handling of road types.
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
Road specific functions.
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:217
@ Roadstop
Required: Bay stop surface.
Definition road.h:49
@ Ground
Required: Main group of ground images.
Definition road.h:41
@ Overlay
Optional: Images for overlaying track.
Definition road.h:40
RoadType GetRoadType(Tile t, RoadTramType rtt)
Get the road type for the given RoadTramType.
Definition road_map.h:175
RoadType
The different roadtypes we support.
Definition road_type.h:24
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:29
RoadTramType
The different types of road type.
Definition road_type.h:38
@ 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.
void DrawCommonTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32_t orig_offset, uint32_t newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
Draws a tile sprite sequence in the GUI.
Definition sprite.cpp:86
PaletteID GroundSpritePaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
Applies PALETTE_MODIFIER_COLOUR to a palette entry of a ground sprite.
Definition sprite.h:207
static const SpriteID SPR_TRAMWAY_TRAM
Definition sprites.h:304
static const SpriteID SPR_ROAD_PAVED_STRAIGHT_X
Definition sprites.h:660
static constexpr uint8_t SPRITE_WIDTH
number of bits for the sprite number
Definition sprites.h:1722
Base classes/functions for stations.
CargoTypes GetCargoWaitingMask(const Station *st)
Get a mask of the cargo types that have cargo waiting at the station.
Functions related to stations.
StationType GetStationType(Tile t)
Get the station type of this tile.
Definition station_map.h:42
StationGfx GetStationGfx(Tile t)
Get the station graphics of this tile.
Definition station_map.h:66
uint GetCustomRoadStopSpecIndex(Tile t)
Get the custom road stop spec for this tile.
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:26
bool IsCustomRoadStopSpecIndex(Tile t)
Is there a custom road stop spec on this tile?
bool IsAnyRoadStopTile(Tile t)
Is tile t a road stop station?
TriggerArea
Scope of station/roadstop animation or randomisation trigger.
@ Platform
Trigger a single platform.
@ Whole
Trigger the entire station.
RoadStopType
Types of RoadStops.
@ Bus
A standard stop for buses.
@ Truck
A standard stop for trucks.
EnumBitSet< StationRandomTrigger, uint8_t > StationRandomTriggers
Bitset of StationRandomTrigger elements.
StationType
Station types.
@ Bus
Road stop for busses.
@ Truck
Road stop for trucks.
@ RoadWaypoint
Waypoint for trucks and busses.
StationRandomTrigger
Randomisation triggers for stations and roadstops.
@ CargoTaken
Trigger station when cargo is completely taken.
StationAnimationTrigger
Animation triggers for stations and roadstops.
@ NewCargo
Trigger station on new cargo arrival.
@ AcceptanceTick
Trigger station every 250 ticks.
@ CargoTaken
Trigger station when cargo is completely taken.
Definition of base types and functions in a cross-platform compatible way.
Helper class for a unified approach to NewGRF animation.
static void ChangeAnimationFrame(CallbackID cb, const RoadStopSpec *spec, BaseStation *obj, TileIndex tile, uint32_t random_bits, uint32_t trigger, int extra_data={})
static void AnimateTile(const RoadStopSpec *spec, BaseStation *obj, TileIndex tile, bool random_animation, int extra_data={})
Base class for all station-ish types.
std::vector< SpecMapping< RoadStopSpec > > roadstop_speclist
List of road stop specs of this station.
StationAnimationTriggers cached_roadstop_anim_triggers
NOSAVE: Combined animation trigger bitmask for road stops, used to determine if trigger processing sh...
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
CargoTypes cached_roadstop_cargo_triggers
NOSAVE: Combined cargo trigger bitmask for road stops.
uint16_t random_bits
Random bits assigned to this station.
std::vector< RoadStopTileData > custom_roadstop_tile_data
List of custom road stop tile data.
StationRandomTriggers waiting_random_triggers
Waiting triggers (NewGRF), shared by all station parts/tiles, road stops, ... essentially useless and...
static constexpr CargoType SG_PURCHASE
Used in purchase lists before an item exists.
static constexpr CargoType SG_DEFAULT
Default type used when no more-specific cargo matches.
static constexpr CargoType SG_DEFAULT_NA
Used only by stations and roads when no more-specific cargo matches.
GrfID grfid
grfid that introduced this entity.
const struct GRFFile * grffile
grf file that introduced this entity
uint16_t local_id
id defined by the grf file for this entity
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID).
Definition newgrf.h:148
Helper class for animation control.
Road stop resolver.
std::optional< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
RoadStopScopeResolver roadstop_scope
The stop scope resolver.
uint8_t view
Station axis.
RoadType roadtype
Road type (used when no tile).
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
StationType type
Station type.
const struct RoadStopSpec * roadstopspec
Station (type) specification.
struct BaseStation * st
Instance of the station.
uint32_t GetRandomTriggers() const override
Get the triggers.
TileIndex tile
Tile of the station.
uint32_t GetRandomBits() const override
Get a few random bits.
Road stop specification.
CargoTypes cargo_triggers
Bitmask of cargo types which cause trigger re-randomizing.
CargoGRFFileProps grf_prop
Link to NewGRF.
ResolverObject & ro
Surrounding resolver object.
Specialization of ResolverObject with type-safe access to RandomTriggers.
static bool IsExpected(const BaseStation *st)
static Station * From(BaseStation *st)
Tile information, used while rendering the tile.
Definition tile_cmd.h:34
TileIndex tile
Tile index.
Definition tile_cmd.h:36
Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
SpriteLayoutProcessor ProcessRegisters(const ResolverObject &object, uint8_t *stage) const
Process registers and the construction stage into the sprite layout.
Scope resolver for a town.
Definition newgrf_town.h:22
Town data structure.
Definition town.h:64
TileIndex xy
town center tile
Definition town.h:65
Generic 'commands' that can be performed on all tiles.
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
Definition of the game-calendar-timer.
Base of the town class.
Town * ClosestTownFromTile(TileIndex tile, uint threshold)
Return the town closest (in distance or ownership) to a given tile, within a given threshold.
HouseZone GetTownRadiusGroup(const Town *t, TileIndex tile)
Returns the bit corresponding to the town zone of the specified tile.
Functions related to (drawing on) viewports.
@ Station
station encountered (could be a target next time)
Definition yapf_type.hpp:26