OpenTTD Source 20250717-master-g55605ae8f2
newgrf_act0_roadstops.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
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../newgrf_engine.h"
13#include "../newgrf_roadstop.h"
14#include "newgrf_bytereader.h"
15#include "newgrf_internal.h"
17
18#include "../safeguards.h"
19
27{
29
30 switch (prop) {
31 case 0x09:
32 case 0x0C:
33 case 0x0F:
34 case 0x11:
35 buf.ReadByte();
36 break;
37
38 case 0x0A:
39 case 0x0B:
40 case 0x0E:
41 case 0x10:
42 case 0x15:
43 buf.ReadWord();
44 break;
45
46 case 0x08:
47 case 0x0D:
48 case 0x12:
49 buf.ReadDWord();
50 break;
51
52 case 0x16: // Badge list
53 SkipBadgeList(buf);
54 break;
55
56 default:
57 ret = CIR_UNKNOWN;
58 break;
59 }
60
61 return ret;
62}
63
64static ChangeInfoResult RoadStopChangeInfo(uint first, uint last, int prop, ByteReader &buf)
65{
67
68 if (last > NUM_ROADSTOPS_PER_GRF) {
69 GrfMsg(1, "RoadStopChangeInfo: RoadStop {} is invalid, max {}, ignoring", last, NUM_ROADSTOPS_PER_GRF);
70 return CIR_INVALID_ID;
71 }
72
73 if (_cur_gps.grffile->roadstops.size() < last) _cur_gps.grffile->roadstops.resize(last);
74
75 for (uint id = first; id < last; ++id) {
76 auto &rs = _cur_gps.grffile->roadstops[id];
77
78 if (rs == nullptr && prop != 0x08) {
79 GrfMsg(1, "RoadStopChangeInfo: Attempt to modify undefined road stop {}, ignoring", id);
81 if (cir > ret) ret = cir;
82 continue;
83 }
84
85 switch (prop) {
86 case 0x08: { // Road Stop Class ID
87 if (rs == nullptr) {
88 rs = std::make_unique<RoadStopSpec>();
89 }
90
91 uint32_t classid = buf.ReadDWord();
92 rs->class_index = RoadStopClass::Allocate(std::byteswap(classid));
93 break;
94 }
95
96 case 0x09: // Road stop type
97 rs->stop_type = (RoadStopAvailabilityType)buf.ReadByte();
98 break;
99
100 case 0x0A: // Road Stop Name
101 AddStringForMapping(GRFStringID{buf.ReadWord()}, &rs->name);
102 break;
103
104 case 0x0B: // Road Stop Class name
105 AddStringForMapping(GRFStringID{buf.ReadWord()}, [rs = rs.get()](StringID str) { RoadStopClass::Get(rs->class_index)->name = str; });
106 break;
107
108 case 0x0C: // The draw modes
109 rs->draw_mode = static_cast<RoadStopDrawModes>(buf.ReadByte());
110 break;
111
112 case 0x0D: // Cargo types for random triggers
113 rs->cargo_triggers = TranslateRefitMask(buf.ReadDWord());
114 break;
115
116 case 0x0E: // Animation info
117 rs->animation.frames = buf.ReadByte();
118 rs->animation.status = static_cast<AnimationStatus>(buf.ReadByte());
119 break;
120
121 case 0x0F: // Animation speed
122 rs->animation.speed = buf.ReadByte();
123 break;
124
125 case 0x10: // Animation triggers
126 rs->animation.triggers = static_cast<StationAnimationTriggers>(buf.ReadWord());
127 break;
128
129 case 0x11: // Callback mask
130 rs->callback_mask = static_cast<RoadStopCallbackMasks>(buf.ReadByte());
131 break;
132
133 case 0x12: // General flags
134 rs->flags = static_cast<RoadStopSpecFlags>(buf.ReadDWord()); // Future-proofing, size this as 4 bytes, but we only need two byte's worth of flags at present
135 break;
136
137 case 0x15: // Cost multipliers
138 rs->build_cost_multiplier = buf.ReadByte();
139 rs->clear_cost_multiplier = buf.ReadByte();
140 break;
141
142 case 0x16: // Badge list
143 rs->badges = ReadBadgeList(buf, GSF_ROADSTOPS);
144 break;
145
146 default:
147 ret = CIR_UNKNOWN;
148 break;
149 }
150 }
151
152 return ret;
153}
154
156template <> ChangeInfoResult GrfChangeInfoHandler<GSF_ROADSTOPS>::Activation(uint first, uint last, int prop, ByteReader &buf) { return RoadStopChangeInfo(first, last, prop, buf); }
Class to read from a NewGRF file.
uint32_t ReadDWord()
Read a single DWord (32 bits).
uint16_t ReadWord()
Read a single Word (16 bits).
uint8_t ReadByte()
Read a single byte (8 bits).
StringID name
Name of this class.
static NewGRFClass * Get(Tindex class_index)
Get a particular class.
static Tindex Allocate(uint32_t global_id)
Allocate a class with a given global class ID.
CargoTypes TranslateRefitMask(uint32_t refit_mask)
Translate the refit mask.
Definition newgrf.cpp:311
std::vector< BadgeID > ReadBadgeList(ByteReader &buf, GrfSpecFeature feature)
Read a list of badges.
void SkipBadgeList(ByteReader &buf)
Skip a list of badges.
static ChangeInfoResult IgnoreRoadStopProperty(uint prop, ByteReader &buf)
Ignore properties for roadstops.
NewGRF buffer reader definition.
NewGRF internal processing state.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
@ CIR_UNKNOWN
Variable is unknown.
@ CIR_UNHANDLED
Variable was parsed but unread.
@ CIR_SUCCESS
Variable was parsed and read.
RoadStopAvailabilityType
Various different options for availability, restricting the roadstop to be only for busses or for tru...
static const int NUM_ROADSTOPS_PER_GRF
The maximum amount of roadstops a single GRF is allowed to add.
void AddStringForMapping(GRFStringID source, std::function< void(StringID)> &&func)
Record a static StringID for getting translated later.
NewGRF string mapping definition.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
GRF feature handler.
GRFFile * grffile
Currently processed GRF file.