OpenTTD Source: src/newgrf/newgrf_act0_cargo.cpp Source File

OpenTTD Source 15.0-RC2

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
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../newgrf_cargo.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
15#include "newgrf_stringmapping.h"
16
17#include "../safeguards.h"
18
27 static ChangeInfoResult CargoReserveInfo(uint first, uint last, int prop, ByteReader &buf)
28{
29 ChangeInfoResult ret = CIR_SUCCESS;
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_gps.grffile;
44 SetBit(_cargo_mask, id);
45 } else {
46 ClrBit(_cargo_mask, id);
47 }
48 BuildCargoLabelMap();
49 break;
50
51 case 0x09: // String ID for cargo type name
52 AddStringForMapping(GRFStringID{buf.ReadWord()}, &cs->name);
53 break;
54
55 case 0x0A: // String for 1 unit of cargo
56 AddStringForMapping(GRFStringID{buf.ReadWord()}, &cs->name_single);
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. */
64 AddStringForMapping(GRFStringID{buf.ReadWord()}, &cs->units_volume);
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. */
72 AddStringForMapping(GRFStringID{buf.ReadWord()}, &cs->quantifier);
73 break;
74
75 case 0x0D: // String for two letter cargo abbreviation
76 AddStringForMapping(GRFStringID{buf.ReadWord()}, &cs->abbrev);
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 = PixelColour{buf.ReadByte()};
101 break;
102
103 case 0x14: // Colour for cargo graph
104 cs->legend_colour = PixelColour{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())};
117 BuildCargoLabelMap();
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 multiplier
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); }
177template <> ChangeInfoResult GrfChangeInfoHandler<GSF_CARGOES>::Activation(uint, uint, int, ByteReader &) { return CIR_UNHANDLED; }
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
ClrBit
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Definition bitmath_func.hpp:139
NUM_CARGO
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
BuildCargoLabelMap
void BuildCargoLabelMap()
Build cargo label map.
Definition cargotype.cpp:132
_cargo_mask
CargoTypes _cargo_mask
Bitmask of cargo types available.
Definition cargotype.cpp:31
TPE_NONE
@ TPE_NONE
Town will not produce this cargo type.
Definition cargotype.h:36
TPE_PASSENGERS
@ TPE_PASSENGERS
Cargo behaves passenger-like for production.
Definition cargotype.h:37
TPE_MAIL
@ TPE_MAIL
Cargo behaves mail-like for production.
Definition cargotype.h:38
TAE_GOODS
@ TAE_GOODS
Cargo behaves goods/candy-like.
Definition cargotype.h:27
TAE_NONE
@ TAE_NONE
Cargo has no effect.
Definition cargotype.h:24
TAE_PASSENGERS
@ TAE_PASSENGERS
Cargo behaves passenger-like.
Definition cargotype.h:25
TAE_MAIL
@ TAE_MAIL
Cargo behaves mail-like.
Definition cargotype.h:26
TAE_FOOD
@ TAE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:29
TAE_WATER
@ TAE_WATER
Cargo behaves water-like.
Definition cargotype.h:28
ByteReader
Class to read from a NewGRF file.
Definition newgrf_bytereader.h:18
ByteReader::ReadDWord
uint32_t ReadDWord()
Read a single DWord (32 bits).
Definition newgrf_bytereader.h:66
ByteReader::ReadWord
uint16_t ReadWord()
Read a single Word (16 bits).
Definition newgrf_bytereader.h:45
ByteReader::ReadByte
uint8_t ReadByte()
Read a single byte (8 bits).
Definition newgrf_bytereader.h:34
EnumBitSet< CargoClass, uint16_t >
CargoReserveInfo
static ChangeInfoResult CargoReserveInfo(uint first, uint last, int prop, ByteReader &buf)
Define properties for cargoes.
Definition newgrf_act0_cargo.cpp:27
newgrf_bytereader.h
NewGRF buffer reader definition.
newgrf_internal.h
NewGRF internal processing state.
ChangeInfoResult
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
Definition newgrf_internal.h:20
CIR_INVALID_ID
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
Definition newgrf_internal.h:25
CIR_UNKNOWN
@ CIR_UNKNOWN
Variable is unknown.
Definition newgrf_internal.h:24
CIR_UNHANDLED
@ CIR_UNHANDLED
Variable was parsed but unread.
Definition newgrf_internal.h:23
CIR_SUCCESS
@ CIR_SUCCESS
Variable was parsed and read.
Definition newgrf_internal.h:21
AddStringForMapping
void AddStringForMapping(GRFStringID source, std::function< void(StringID)> &&func)
Record a static StringID for getting translated later.
Definition newgrf_stringmapping.cpp:42
newgrf_stringmapping.h
NewGRF string mapping definition.
CargoSpec
Specification of a cargo type.
Definition cargotype.h:74
CargoSpec::initial_payment
int32_t initial_payment
Initial payment rate before inflation is applied.
Definition cargotype.h:82
CargoSpec::classes
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:81
CargoSpec::multiplier
uint16_t multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition cargotype.h:80
CargoSpec::units_volume
StringID units_volume
Name of a single unit of cargo of this type.
Definition cargotype.h:93
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
CargoSpec::abbrev
StringID abbrev
Two letter abbreviation for this cargo type.
Definition cargotype.h:95
CargoSpec::bitnum
uint8_t bitnum
Cargo bit number, is INVALID_CARGO_BITNUM for a non-used spec.
Definition cargotype.h:76
CargoSpec::quantifier
StringID quantifier
Text for multiple units of cargo of this type.
Definition cargotype.h:94
CargoSpec::sprite
SpriteID sprite
Icon to display this cargo type, may be 0xFFF (which means to resolve an action123 chain).
Definition cargotype.h:97
CargoSpec::is_freight
bool is_freight
Cargo type is considered to be freight (affects train freight multiplier).
Definition cargotype.h:85
CargoSpec::weight
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition cargotype.h:79
CargoSpec::label
CargoLabel label
Unique label of the cargo type.
Definition cargotype.h:75
CargoSpec::grffile
const struct GRFFile * grffile
NewGRF where group belongs to.
Definition cargotype.h:99
CargoSpec::name
StringID name
Name of this type of cargo.
Definition cargotype.h:91
CargoSpec::town_production_effect
TownProductionEffect town_production_effect
The effect on town cargo production.
Definition cargotype.h:87
CargoSpec::town_acceptance_effect
TownAcceptanceEffect town_acceptance_effect
The effect that delivering this cargo type has on towns. Also affects destination of subsidies.
Definition cargotype.h:86
CargoSpec::callback_mask
CargoCallbackMasks callback_mask
Bitmask of cargo callbacks that have to be called.
Definition cargotype.h:89
CargoSpec::town_production_multiplier
uint16_t town_production_multiplier
Town production multiplier, if commanded by TownProductionEffect.
Definition cargotype.h:88
CargoSpec::IsValid
bool IsValid() const
Tests for validity of this cargospec.
Definition cargotype.h:118
CargoSpec::name_single
StringID name_single
Name of a single entity of this type of cargo.
Definition cargotype.h:92
GrfChangeInfoHandler
GRF feature handler.
Definition newgrf_internal.h:30
GrfProcessingState::grffile
GRFFile * grffile
Currently processed GRF file.
Definition newgrf_internal.h:67
PixelColour
Colour for pixel/line drawing.
Definition gfx_type.h:405
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer >