OpenTTD Source 20261112-master-gbcbec5e63f
newgrf_act0_cargo.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 "../newgrf_cargo.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
16#include "../cargotype.h"
17
18#include "../safeguards.h"
19
27static void MaybeInstallFallbackCargoLabel(uint id, uint last, CargoLabel label)
28{
29 if (_cur_gps.grffile->cargo_list.empty()) {
30 /* Cargo translation table isn't configured yet, assume it won't be and configure the cargo list as a fallback list. */
31 auto default_cargo_list = GetCargoTranslationTable(*_cur_gps.grffile);
32 _cur_gps.grffile->cargo_list.assign(default_cargo_list.begin(), default_cargo_list.end());
33 _cur_gps.grffile->cargo_list_is_fallback = true;
34 }
35
36 if (_cur_gps.grffile->cargo_list_is_fallback) {
37 /* Automatically fill fallback cargo list with the defined label, resizing as needed. */
38 if (_cur_gps.grffile->cargo_list.size() < last) _cur_gps.grffile->cargo_list.resize(last, CT_INVALID);
39 _cur_gps.grffile->cargo_list[id] = label;
40 }
41}
42
51static ChangeInfoResult CargoReserveInfo(uint first, uint last, int prop, ByteReader &buf)
52{
54
55 if (last > NUM_CARGO) {
56 GrfMsg(Severity::Warning, "CargoChangeInfo: Cargo type {} out of range (max {})", last, NUM_CARGO - 1);
58 }
59
60 for (uint id = first; id < last; ++id) {
61 CargoSpec *cs = CargoSpec::Get(id);
62
63 switch (prop) {
64 case 0x08: // Bit number of cargo
65 cs->bitnum = buf.ReadByte();
66 if (cs->IsValid()) {
67 cs->grffile = _cur_gps.grffile;
68 _cargo_mask.Set(cs->Index());
69 } else {
70 _cargo_mask.Reset(cs->Index());
71 }
73 break;
74
75 case 0x09: // String ID for cargo type name
77 break;
78
79 case 0x0A: // String for 1 unit of cargo
81 break;
82
83 case 0x0B: // String for singular quantity of cargo (e.g. 1 tonne of coal)
84 case 0x1B: // String for cargo units
85 /* String for units of cargo. This is different in OpenTTD
86 * (e.g. tonnes) to TTDPatch (e.g. {COMMA} tonne of coal).
87 * Property 1B is used to set OpenTTD's behaviour. */
89 break;
90
91 case 0x0C: // String for plural quantity of cargo (e.g. 10 tonnes of coal)
92 case 0x1C: // String for any amount of cargo
93 /* Strings for an amount of cargo. This is different in OpenTTD
94 * (e.g. {WEIGHT} of coal) to TTDPatch (e.g. {COMMA} tonnes of coal).
95 * Property 1C is used to set OpenTTD's behaviour. */
97 break;
98
99 case 0x0D: // String for two letter cargo abbreviation
101 break;
102
103 case 0x0E: // Sprite ID for cargo icon
104 cs->sprite = buf.ReadWord();
105 break;
106
107 case 0x0F: // Weight of one unit of cargo
108 cs->weight = buf.ReadByte();
109 break;
110
111 case 0x10: // Used for payment calculation
112 cs->transit_periods[0] = buf.ReadByte();
113 break;
114
115 case 0x11: // Used for payment calculation
116 cs->transit_periods[1] = buf.ReadByte();
117 break;
118
119 case 0x12: // Base cargo price
120 cs->initial_payment = buf.ReadDWord();
121 break;
122
123 case 0x13: // Colour for station rating bars
125 break;
126
127 case 0x14: // Colour for cargo graph
129 break;
130
131 case 0x15: // Freight status
132 cs->is_freight = (buf.ReadByte() != 0);
133 break;
134
135 case 0x16: // Cargo classes
136 cs->classes = CargoClasses{buf.ReadWord()};
137 break;
138
139 case 0x17: // Cargo label
140 cs->label = buf.ReadLabel<CargoLabel>();
143 break;
144
145 case 0x18: { // Town growth substitute type
146 uint8_t substitute_type = buf.ReadByte();
147
148 switch (substitute_type) {
154 default:
155 GrfMsg(Severity::Error, "CargoChangeInfo: Unknown town growth substitute value {}, setting to none.", substitute_type);
156 [[fallthrough]];
158 }
159 break;
160 }
161
162 case 0x19: // Town growth coefficient
163 buf.ReadWord();
164 break;
165
166 case 0x1A: // Bitmask of callbacks to use
168 break;
169
170 case 0x1D: // Vehicle capacity multiplier
171 cs->multiplier = std::max<uint16_t>(1u, buf.ReadWord());
172 break;
173
174 case 0x1E: { // Town production substitute type
175 uint8_t substitute_type = buf.ReadByte();
176
177 switch (substitute_type) {
180 default:
181 GrfMsg(Severity::Error, "CargoChangeInfo: Unknown town production substitute value {}, setting to none.", substitute_type);
182 [[fallthrough]];
184 }
185 break;
186 }
187
188 case 0x1F: // Town production multiplier
189 cs->town_production_multiplier = std::max<uint16_t>(1U, buf.ReadWord());
190 break;
191
192 default:
194 break;
195 }
196 }
197
198 return ret;
199}
200
202template <> ChangeInfoResult GrfChangeInfoHandler<GrfSpecFeature::Cargoes>::Reserve(uint first, uint last, int prop, ByteReader &buf) { return CargoReserveInfo(first, last, prop, buf); }
static constexpr CargoLabel CT_INVALID
Invalid cargo type.
Definition cargo_type.h:74
Label< struct CargoLabelTag > CargoLabel
Globally unique label of a cargo type.
Definition cargo_type.h:17
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:77
void BuildCargoLabelMap()
Build cargo label map.
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition cargotype.cpp:30
Types/functions related to cargoes.
@ Mail
Cargo behaves mail-like for production.
Definition cargotype.h:38
@ Passengers
Cargo behaves passenger-like for production.
Definition cargotype.h:37
@ None
Town will not produce this cargo type.
Definition cargotype.h:36
EnumBitSet< CargoClass, uint16_t > CargoClasses
Bitset of CargoClass elements.
Definition cargotype.h:69
@ Food
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:28
@ Water
Cargo behaves water-like.
Definition cargotype.h:27
@ Mail
Cargo behaves mail-like.
Definition cargotype.h:25
@ Passengers
Cargo behaves passenger-like.
Definition cargotype.h:24
@ None
Cargo has no effect.
Definition cargotype.h:23
@ Goods
Cargo behaves goods/candy-like.
Definition cargotype.h:26
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).
T ReadLabel()
Read a label.
uint8_t ReadByte()
Read a single byte (8 bits).
Functions related to debugging.
@ Warning
Warning, wrong but okay if you don't know.
Definition debug_type.h:17
@ Error
Error, but we are recovering.
Definition debug_type.h:16
std::span< const CargoLabel > GetCargoTranslationTable(const GRFFile &grffile)
Get the cargo translation table to use for the given GRF file.
Definition newgrf.cpp:527
static ChangeInfoResult CargoReserveInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for cargoes.
static void MaybeInstallFallbackCargoLabel(uint id, uint last, CargoLabel label)
Install cargo label in a fallback cargo list, if a NewGRF-defined cargo list is not present.
NewGRF buffer reader definition.
EnumBitSet< CargoCallbackMask, uint8_t > CargoCallbackMasks
Bitset of CargoCallbackMask elements.
Cargo support for NewGRFs.
NewGRF internal processing state.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ Success
Variable was parsed and read.
@ Unhandled
Variable was parsed but unread.
@ Unknown
Variable is unknown.
@ InvalidId
Attempt to modify an invalid ID.
void AddStringForMapping(GRFStringID source, std::function< void(StringID)> &&func)
Record a static StringID for getting translated later.
NewGRF string mapping definition.
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer > GRFStringID
Type for GRF-internal string IDs.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Specification of a cargo type.
Definition cargotype.h:76
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition cargotype.h:84
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:83
uint16_t multiplier
Capacity multiplier for vehicles. (8 fractional bits).
Definition cargotype.h:82
StringID units_volume
Name of a single unit of cargo of this type.
Definition cargotype.h:95
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:140
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:97
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:78
StringID quantifier
Text for multiple units of cargo of this type.
Definition cargotype.h:96
CargoType Index() const
Determines index of this cargospec.
Definition cargotype.h:110
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition cargotype.h:99
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition cargotype.h:87
PixelColour legend_colour
Colour in legends and graphs.
Definition cargotype.h:79
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition cargotype.h:81
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:77
const struct GRFFile * grffile
NewGRF where group belongs to.
Definition cargotype.h:101
PixelColour rating_colour
Colour used for box representing the amount of cargo waiting at the station in the station rating vie...
Definition cargotype.h:80
uint8_t transit_periods[2]
Values used to calculate timefactor for cargo payment alghoritm.
Definition cargotype.h:85
StringID name
Name of this type of cargo.
Definition cargotype.h:93
TownProductionEffect town_production_effect
The effect on town cargo production.
Definition cargotype.h:89
TownAcceptanceEffect town_acceptance_effect
The effect that delivering this cargo type has on towns. Also affects destination of subsidies.
Definition cargotype.h:88
CargoCallbackMasks callback_mask
Bitmask of cargo callbacks that have to be called.
Definition cargotype.h:91
uint16_t town_production_multiplier
Town production multiplier, if commanded by TownProductionEffect.
Definition cargotype.h:90
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:120
StringID name_single
Name of a single entity of this type of cargo.
Definition cargotype.h:94
static ChangeInfoResult Reserve(uint first, uint last, int prop, ByteReader &buf)
Implementation of the GrfLoadingStage::Reserve stage of this feature.
static ChangeInfoResult Activation(uint first, uint last, int prop, ByteReader &buf)
Implementation of the GrfLoadingStage::Activation stage of this feature.
Colour for pixel/line drawing.
Definition gfx_type.h:307