OpenTTD Source 20250331-master-g3c15e0c889
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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../newgrf_cargo.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
16
17#include "../safeguards.h"
18
27static ChangeInfoResult CargoReserveInfo(uint first, uint last, int prop, ByteReader &buf)
28{
30
31 if (last > NUM_CARGO) {
32 GrfMsg(2, "CargoChangeInfo: Cargo type {} out of range (max {})", last, NUM_CARGO - 1);
33 return CIR_INVALID_ID;
34 }
35
36 for (uint id = first; id < last; ++id) {
37 CargoSpec *cs = CargoSpec::Get(id);
38
39 switch (prop) {
40 case 0x08: // Bit number of cargo
41 cs->bitnum = buf.ReadByte();
42 if (cs->IsValid()) {
43 cs->grffile = _cur.grffile;
45 } else {
47 }
49 break;
50
51 case 0x09: // String ID for cargo type name
53 break;
54
55 case 0x0A: // String for 1 unit of cargo
57 break;
58
59 case 0x0B: // String for singular quantity of cargo (e.g. 1 tonne of coal)
60 case 0x1B: // String for cargo units
61 /* String for units of cargo. This is different in OpenTTD
62 * (e.g. tonnes) to TTDPatch (e.g. {COMMA} tonne of coal).
63 * Property 1B is used to set OpenTTD's behaviour. */
65 break;
66
67 case 0x0C: // String for plural quantity of cargo (e.g. 10 tonnes of coal)
68 case 0x1C: // String for any amount of cargo
69 /* Strings for an amount of cargo. This is different in OpenTTD
70 * (e.g. {WEIGHT} of coal) to TTDPatch (e.g. {COMMA} tonnes of coal).
71 * Property 1C is used to set OpenTTD's behaviour. */
73 break;
74
75 case 0x0D: // String for two letter cargo abbreviation
77 break;
78
79 case 0x0E: // Sprite ID for cargo icon
80 cs->sprite = buf.ReadWord();
81 break;
82
83 case 0x0F: // Weight of one unit of cargo
84 cs->weight = buf.ReadByte();
85 break;
86
87 case 0x10: // Used for payment calculation
88 cs->transit_periods[0] = buf.ReadByte();
89 break;
90
91 case 0x11: // Used for payment calculation
92 cs->transit_periods[1] = buf.ReadByte();
93 break;
94
95 case 0x12: // Base cargo price
96 cs->initial_payment = buf.ReadDWord();
97 break;
98
99 case 0x13: // Colour for station rating bars
100 cs->rating_colour = buf.ReadByte();
101 break;
102
103 case 0x14: // Colour for cargo graph
104 cs->legend_colour = buf.ReadByte();
105 break;
106
107 case 0x15: // Freight status
108 cs->is_freight = (buf.ReadByte() != 0);
109 break;
110
111 case 0x16: // Cargo classes
112 cs->classes = CargoClasses{buf.ReadWord()};
113 break;
114
115 case 0x17: // Cargo label
116 cs->label = CargoLabel{std::byteswap(buf.ReadDWord())};
118 break;
119
120 case 0x18: { // Town growth substitute type
121 uint8_t substitute_type = buf.ReadByte();
122
123 switch (substitute_type) {
124 case 0x00: cs->town_acceptance_effect = TAE_PASSENGERS; break;
125 case 0x02: cs->town_acceptance_effect = TAE_MAIL; break;
126 case 0x05: cs->town_acceptance_effect = TAE_GOODS; break;
127 case 0x09: cs->town_acceptance_effect = TAE_WATER; break;
128 case 0x0B: cs->town_acceptance_effect = TAE_FOOD; break;
129 default:
130 GrfMsg(1, "CargoChangeInfo: Unknown town growth substitute value {}, setting to none.", substitute_type);
131 [[fallthrough]];
132 case 0xFF: cs->town_acceptance_effect = TAE_NONE; break;
133 }
134 break;
135 }
136
137 case 0x19: // Town growth coefficient
138 buf.ReadWord();
139 break;
140
141 case 0x1A: // Bitmask of callbacks to use
142 cs->callback_mask = static_cast<CargoCallbackMasks>(buf.ReadByte());
143 break;
144
145 case 0x1D: // Vehicle capacity muliplier
146 cs->multiplier = std::max<uint16_t>(1u, buf.ReadWord());
147 break;
148
149 case 0x1E: { // Town production substitute type
150 uint8_t substitute_type = buf.ReadByte();
151
152 switch (substitute_type) {
153 case 0x00: cs->town_production_effect = TPE_PASSENGERS; break;
154 case 0x02: cs->town_production_effect = TPE_MAIL; break;
155 default:
156 GrfMsg(1, "CargoChangeInfo: Unknown town production substitute value {}, setting to none.", substitute_type);
157 [[fallthrough]];
158 case 0xFF: cs->town_production_effect = TPE_NONE; break;
159 }
160 break;
161 }
162
163 case 0x1F: // Town production multiplier
164 cs->town_production_multiplier = std::max<uint16_t>(1U, buf.ReadWord());
165 break;
166
167 default:
168 ret = CIR_UNKNOWN;
169 break;
170 }
171 }
172
173 return ret;
174}
175
176template <> ChangeInfoResult GrfChangeInfoHandler<GSF_CARGOES>::Reserve(uint first, uint last, int prop, ByteReader &buf) { return CargoReserveInfo(first, last, prop, buf); }
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:75
void BuildCargoLabelMap()
Build cargo label map.
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition cargotype.cpp:33
@ TPE_NONE
Town will not produce this cargo type.
Definition cargotype.h:36
@ TPE_PASSENGERS
Cargo behaves passenger-like for production.
Definition cargotype.h:37
@ TPE_MAIL
Cargo behaves mail-like for production.
Definition cargotype.h:38
@ TAE_GOODS
Cargo behaves goods/candy-like.
Definition cargotype.h:27
@ TAE_NONE
Cargo has no effect.
Definition cargotype.h:24
@ TAE_PASSENGERS
Cargo behaves passenger-like.
Definition cargotype.h:25
@ TAE_MAIL
Cargo behaves mail-like.
Definition cargotype.h:26
@ TAE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:29
@ TAE_WATER
Cargo behaves water-like.
Definition cargotype.h:28
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).
static ChangeInfoResult CargoReserveInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for cargoes.
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.
void AddStringForMapping(GRFStringID source, std::function< void(StringID)> &&func)
Record a static StringID for getting translated later.
NewGRF string mapping definition.
Specification of a cargo type.
Definition cargotype.h:74
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition cargotype.h:82
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:81
uint16_t multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition cargotype.h:80
StringID units_volume
Name of a single unit of cargo of this type.
Definition cargotype.h:93
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:95
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:76
StringID quantifier
Text for multiple units of cargo of this type.
Definition cargotype.h:94
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition cargotype.h:97
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition cargotype.h:85
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition cargotype.h:79
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:75
const struct GRFFile * grffile
NewGRF where group belongs to.
Definition cargotype.h:99
StringID name
Name of this type of cargo.
Definition cargotype.h:91
TownProductionEffect town_production_effect
The effect on town cargo production.
Definition cargotype.h:87
TownAcceptanceEffect town_acceptance_effect
The effect that delivering this cargo type has on towns. Also affects destination of subsidies.
Definition cargotype.h:86
CargoCallbackMasks callback_mask
Bitmask of cargo callbacks that have to be called.
Definition cargotype.h:89
uint16_t town_production_multiplier
Town production multiplier, if commanded by TownProductionEffect.
Definition cargotype.h:88
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:118
StringID name_single
Name of a single entity of this type of cargo.
Definition cargotype.h:92
GRF feature handler.
GRFFile * grffile
Currently processed GRF file.