OpenTTD Source 20261112-master-gbcbec5e63f
newgrf_town.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 "town.h"
13#include "newgrf_town.h"
14#include "newgrf_cargo.h"
16
17#include "safeguards.h"
18
27static uint16_t TownProductionHistoryHelper(const Town *t, CargoLabel label, uint period, auto proj)
28{
29 auto it = t->GetCargoSupplied(GetCargoTypeByLabel(label));
30 if (it == std::end(t->supplied)) return 0;
31
32 return ClampTo<uint16_t>(std::invoke(proj, it->history[period]));
33}
34
44static uint32_t TownProductionHistoryHelperCargoID(const Town *t, uint32_t parameter, const GRFFile *grffile, uint period, auto proj)
45{
46 CargoType cargo = GetCargoTranslation(parameter, grffile);
47 if (!IsValidCargoType(cargo)) return 0;
48
49 auto it = t->GetCargoSupplied(cargo);
50 if (it == std::end(t->supplied)) return 0;
51
52 return ClampTo<uint32_t>(std::invoke(proj, it->history[period]));
53}
54
64static uint32_t TownAcceptanceHistoryHelperCargoID(const Town *t, uint32_t parameter, const GRFFile *grffile, uint period, auto proj)
65{
66 CargoType cargo = GetCargoTranslation(parameter, grffile);
67 if (!IsValidCargoType(cargo)) return 0;
68
69 auto it = t->GetCargoAccepted(cargo);
70 if (it == std::end(t->accepted)) return 0;
71
72 return ClampTo<uint32_t>(std::invoke(proj, it->history[period]));
73}
74
82static uint32_t GetNearbyTileInformation(uint8_t parameter, TileIndex tile, bool grf_version8)
83{
84 tile = GetNearbyTile(parameter, tile);
85 return GetNearbyTileInformation(tile, grf_version8);
86}
87
88/* virtual */ uint32_t TownScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
89{
90 if (this->t == nullptr) {
91 available = false;
92 return UINT_MAX;
93 }
94
95 switch (variable) {
96 /* Larger towns */
97 case 0x40:
98 if (_settings_game.economy.larger_towns == 0) return 2;
99 if (this->t->larger_town) return 1;
100 return 0;
101
102 /* Town index */
103 case 0x41: return this->t->index.base();
104
105 /* Additional town information: (for now just) road layout */
106 case 0x42: return to_underlying(this->t->layout);
107
108 /* Number of nearby stations */
109 case 0x43: return ClampTo<uint16_t>(this->t->stations_near.size());
110
111 /* Land info for nearby tiles. */
112 case 0x60: return GetNearbyTileInformation(parameter, this->t->xy, this->ro.grffile->grf_version >= 8);
113
114 /* Recent cargo production and transport, by cargo label */
115 case 0x61: return TownProductionHistoryHelperCargoID(this->t, parameter, this->ro.grffile, THIS_MONTH, &Town::SuppliedHistory::production);
116 case 0x62: return TownProductionHistoryHelperCargoID(this->t, parameter, this->ro.grffile, THIS_MONTH, &Town::SuppliedHistory::transported);
117 case 0x63: return TownProductionHistoryHelperCargoID(this->t, parameter, this->ro.grffile, LAST_MONTH, &Town::SuppliedHistory::production);
118 case 0x64: return TownProductionHistoryHelperCargoID(this->t, parameter, this->ro.grffile, LAST_MONTH, &Town::SuppliedHistory::transported);
119
120 /* Recent cargo acceptance, by cargo label */
121 case 0x65: return TownAcceptanceHistoryHelperCargoID(this->t, parameter, this->ro.grffile, THIS_MONTH, &Town::AcceptedHistory::accepted);
122 case 0x66: return TownAcceptanceHistoryHelperCargoID(this->t, parameter, this->ro.grffile, LAST_MONTH, &Town::AcceptedHistory::accepted);
123
124 /* Get a variable from the persistent storage */
125 case 0x7C: {
126 /* Check the persistent storage for the GrfID stored in register 100h. */
127 GrfID grfid = UnflattenNewGRFLabel<GrfID>(this->ro.GetRegister(0x100));
128 if (grfid == INVALID_GRFID) {
129 if (this->ro.grffile == nullptr) return 0;
130 grfid = this->ro.grffile->grfid;
131 }
132
133 for (auto &it : this->t->psa_list) {
134 if (it->grfid == grfid) return it->GetValue(parameter);
135 }
136
137 return 0;
138 }
139
140 /* Town properties */
141 case 0x80: return this->t->xy.base();
142 case 0x81: return GB(this->t->xy.base(), 8, 8);
143 case 0x82: return ClampTo<uint16_t>(this->t->cache.population);
144 case 0x83: return GB(ClampTo<uint16_t>(this->t->cache.population), 8, 8);
145 case 0x8A: return this->t->grow_counter / Ticks::TOWN_GROWTH_TICKS;
146 case 0x92: return this->t->flags.base(); // In original game, 0x92 and 0x93 are one word.
147 case 0x93: return 0;
148 case 0x94: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownEdge)]);
149 case 0x95: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownEdge)]), 8, 8);
150 case 0x96: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownOutskirt)]);
151 case 0x97: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownOutskirt)]), 8, 8);
152 case 0x98: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownOuterSuburb)]);
153 case 0x99: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownOuterSuburb)]), 8, 8);
154 case 0x9A: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownInnerSuburb)]);
155 case 0x9B: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownInnerSuburb)]), 8, 8);
156 case 0x9C: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownCentre)]);
157 case 0x9D: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownCentre)]), 8, 8);
158 case 0x9E: return this->t->ratings[CompanyID{0}];
159 case 0x9F: return GB(this->t->ratings[CompanyID{0}], 8, 8);
160 case 0xA0: return this->t->ratings[CompanyID{1}];
161 case 0xA1: return GB(this->t->ratings[CompanyID{1}], 8, 8);
162 case 0xA2: return this->t->ratings[CompanyID{2}];
163 case 0xA3: return GB(this->t->ratings[CompanyID{2}], 8, 8);
164 case 0xA4: return this->t->ratings[CompanyID{3}];
165 case 0xA5: return GB(this->t->ratings[CompanyID{3}], 8, 8);
166 case 0xA6: return this->t->ratings[CompanyID{4}];
167 case 0xA7: return GB(this->t->ratings[CompanyID{4}], 8, 8);
168 case 0xA8: return this->t->ratings[CompanyID{5}];
169 case 0xA9: return GB(this->t->ratings[CompanyID{5}], 8, 8);
170 case 0xAA: return this->t->ratings[CompanyID{6}];
171 case 0xAB: return GB(this->t->ratings[CompanyID{6}], 8, 8);
172 case 0xAC: return this->t->ratings[CompanyID{7}];
173 case 0xAD: return GB(this->t->ratings[CompanyID{7}], 8, 8);
174 case 0xAE: return this->t->have_ratings.base();
175 case 0xB2: return this->t->statues.base();
176 case 0xB6: return ClampTo<uint16_t>(this->t->cache.num_houses);
177 case 0xB9: return this->t->growth_rate / Ticks::TOWN_GROWTH_TICKS;
179 case 0xBB: return TownProductionHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::production) >> 8;
180 case 0xBC: return TownProductionHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::production);
181 case 0xBD: return TownProductionHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::production) >> 8;
183 case 0xBF: return TownProductionHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::transported) >> 8;
184 case 0xC0: return TownProductionHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::transported);
185 case 0xC1: return TownProductionHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::transported) >> 8;
187 case 0xC3: return TownProductionHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::production) >> 8;
188 case 0xC4: return TownProductionHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::production);
189 case 0xC5: return TownProductionHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::production) >> 8;
191 case 0xC7: return TownProductionHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::transported) >> 8;
192 case 0xC8: return TownProductionHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::transported);
193 case 0xC9: return TownProductionHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::transported) >> 8;
194 case 0xCA: return this->t->GetPercentTransported(GetCargoTypeByLabel(CT_PASSENGERS));
195 case 0xCB: return this->t->GetPercentTransported(GetCargoTypeByLabel(CT_MAIL));
196 case 0xCC: return this->t->received[TownAcceptanceEffect::Food].new_act;
197 case 0xCD: return GB(this->t->received[TownAcceptanceEffect::Food].new_act, 8, 8);
198 case 0xCE: return this->t->received[TownAcceptanceEffect::Water].new_act;
199 case 0xCF: return GB(this->t->received[TownAcceptanceEffect::Water].new_act, 8, 8);
200 case 0xD0: return this->t->received[TownAcceptanceEffect::Food].old_act;
201 case 0xD1: return GB(this->t->received[TownAcceptanceEffect::Food].old_act, 8, 8);
202 case 0xD2: return this->t->received[TownAcceptanceEffect::Water].old_act;
203 case 0xD3: return GB(this->t->received[TownAcceptanceEffect::Water].old_act, 8, 8);
204 case 0xD4: return this->t->road_build_months;
205 case 0xD5: return this->t->fund_buildings_months;
206 }
207
208 this->ro.UnhandledVariable(variable);
209
210 available = false;
211 return UINT_MAX;
212}
213
214/* virtual */ void TownScopeResolver::StorePSA(uint pos, int32_t value)
215{
216 if (this->readonly) return;
217
218 assert(this->t != nullptr);
219 /* We can't store anything if the caller has no #GRFFile. */
220 if (this->ro.grffile == nullptr) return;
221
222 /* Check the persistent storage for the GrfID stored in register 100h. */
223 GrfID grfid = UnflattenNewGRFLabel<GrfID>(this->ro.GetRegister(0x100));
224
225 /* A NewGRF can only write in the persistent storage associated to its own GRFID. */
226 if (grfid == INVALID_GRFID) grfid = this->ro.grffile->grfid;
227 if (grfid != this->ro.grffile->grfid) return;
228
229 /* Check if the storage exists. */
230 for (auto &it : t->psa_list) {
231 if (it->grfid == grfid) {
232 it->StoreValue(pos, value);
233 return;
234 }
235 }
236
237 /* Create a new storage. */
240 psa->StoreValue(pos, value);
241 t->psa_list.push_back(psa);
242}
243
251 : ResolverObject(grffile), town_scope(*this, t, readonly)
252{
253}
254
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Label< struct CargoLabelTag > CargoLabel
Globally unique label of a cargo type.
Definition cargo_type.h:17
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:112
static constexpr CargoLabel CT_MAIL
Cargo label for mail.
Definition cargo_type.h:34
static constexpr CargoLabel CT_PASSENGERS
Cargo label for passengers.
Definition cargo_type.h:32
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
CargoType GetCargoTypeByLabel(CargoLabel label)
Get cargo assigned to a label.
Definition cargotype.h:268
@ Food
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:28
@ Water
Cargo behaves water-like.
Definition cargotype.h:27
static constexpr TimerGameTick::Ticks TOWN_GROWTH_TICKS
Cycle duration for towns trying to grow (this originates from the size of the town array in TTD).
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
@ TownOuterSuburb
Outer suburbs; roads with pavement.
Definition house.h:62
@ TownInnerSuburb
Inner suburbs; roads with pavement and trees.
Definition house.h:63
@ TownOutskirt
Outskirts of a town; roads without pavement.
Definition house.h:61
@ TownEdge
Edge of the town; roads without pavement.
Definition house.h:60
@ TownCentre
Centre of town; roads with pavement and streetlights.
Definition house.h:64
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
constexpr T UnflattenNewGRFLabel(uint32_t value)
Unflatten a NewGRF related label from a 32 bits integer.
Definition newgrf.h:270
@ FakeTowns
Fake town GrfSpecFeature for NewGRF debugging (parent scope).
Definition newgrf.h:107
CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoType.
Cargo support for NewGRFs.
uint32_t GetNearbyTileInformation(TileIndex tile, bool grf_version8)
Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62,...
TileIndex GetNearbyTile(uint8_t parameter, TileIndex tile, bool signed_offsets, Axis axis)
Get the tile at the given offset.
static uint32_t TownProductionHistoryHelperCargoID(const Town *t, uint32_t parameter, const GRFFile *grffile, uint period, auto proj)
Get cargo production/transport history for a town via cargo translation table.
static uint32_t TownAcceptanceHistoryHelperCargoID(const Town *t, uint32_t parameter, const GRFFile *grffile, uint period, auto proj)
Get cargo acceptance history for a town via cargo translation table.
static uint16_t TownProductionHistoryHelper(const Town *t, CargoLabel label, uint period, auto proj)
Get cargo production/transport history for a town via cargo label.
static uint32_t GetNearbyTileInformation(uint8_t parameter, TileIndex tile, bool grf_version8)
Get information about a nearby tile.
Functions to handle the town part of NewGRF towns.
static const GrfID INVALID_GRFID
An invalid NewGRF.
Definition newgrf_type.h:19
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Definition of base types and functions in a cross-platform compatible way.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:126
void StoreValue(uint pos, int32_t value)
Stores some value at a given position.
Class for pooled persistent storage of data.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
ResolverObject(const GRFFile *grffile, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Resolver constructor.
ResolverObject & ro
Surrounding resolver object.
TownScopeResolver town_scope
Scope resolver specific for towns.
Definition newgrf_town.h:43
TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
Resolver for a town.
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
bool readonly
When set, persistent storage of the town is read-only,.
Definition newgrf_town.h:24
void StorePSA(uint reg, int32_t value) override
Store a value into the persistent storage area (PSA).
Town * t
Town of the scope.
Definition newgrf_town.h:23
uint32_t accepted
Total accepted.
Definition town.h:112
uint32_t transported
Total transported.
Definition town.h:93
uint32_t production
Total produced.
Definition town.h:92
Town data structure.
Definition town.h:64
SuppliedCargoes supplied
Cargo statistics about supplied cargo.
Definition town.h:131
AcceptedCargoes::const_iterator GetCargoAccepted(CargoType cargo) const
Get iterator to the storage for an accepted cargo.
Definition town.h:173
AcceptedCargoes accepted
Cargo statistics about accepted cargo.
Definition town.h:132
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
Definition of the tick-based game-timer.
Base of the town class.