OpenTTD Source 20260820-master-g39da062c0c
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"
15
16#include "safeguards.h"
17
18template <typename Tproj>
19static uint16_t TownHistoryHelper(const Town *t, CargoLabel label, uint period, Tproj proj)
20{
21 auto it = t->GetCargoSupplied(GetCargoTypeByLabel(label));
22 if (it == std::end(t->supplied)) return 0;
23
24 return ClampTo<uint16_t>(std::invoke(proj, it->history[period]));
25}
26
34static uint32_t GetNearbyTileInformation(uint8_t parameter, TileIndex tile, bool grf_version8)
35{
36 tile = GetNearbyTile(parameter, tile);
37 return GetNearbyTileInformation(tile, grf_version8);
38}
39
40/* virtual */ uint32_t TownScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
41{
42 if (this->t == nullptr) {
43 available = false;
44 return UINT_MAX;
45 }
46
47 switch (variable) {
48 /* Larger towns */
49 case 0x40:
50 if (_settings_game.economy.larger_towns == 0) return 2;
51 if (this->t->larger_town) return 1;
52 return 0;
53
54 /* Town index */
55 case 0x41: return this->t->index.base();
56
57 /* Additional town information: (for now just) road layout */
58 case 0x42: return to_underlying(this->t->layout);
59
60 /* Number of nearby stations */
61 case 0x43: return ClampTo<uint16_t>(this->t->stations_near.size());
62
63 /* Land info for nearby tiles. */
64 case 0x60: return GetNearbyTileInformation(parameter, this->t->xy, this->ro.grffile->grf_version >= 8);
65
66 /* Get a variable from the persistent storage */
67 case 0x7C: {
68 /* Check the persistent storage for the GrfID stored in register 100h. */
69 GrfID grfid = UnflattenNewGRFLabel<GrfID>(this->ro.GetRegister(0x100));
70 if (grfid == INVALID_GRFID) {
71 if (this->ro.grffile == nullptr) return 0;
72 grfid = this->ro.grffile->grfid;
73 }
74
75 for (auto &it : this->t->psa_list) {
76 if (it->grfid == grfid) return it->GetValue(parameter);
77 }
78
79 return 0;
80 }
81
82 /* Town properties */
83 case 0x80: return this->t->xy.base();
84 case 0x81: return GB(this->t->xy.base(), 8, 8);
85 case 0x82: return ClampTo<uint16_t>(this->t->cache.population);
86 case 0x83: return GB(ClampTo<uint16_t>(this->t->cache.population), 8, 8);
87 case 0x8A: return this->t->grow_counter / Ticks::TOWN_GROWTH_TICKS;
88 case 0x92: return this->t->flags.base(); // In original game, 0x92 and 0x93 are one word.
89 case 0x93: return 0;
100 case 0x9E: return this->t->ratings[CompanyID{0}];
101 case 0x9F: return GB(this->t->ratings[CompanyID{0}], 8, 8);
102 case 0xA0: return this->t->ratings[CompanyID{1}];
103 case 0xA1: return GB(this->t->ratings[CompanyID{1}], 8, 8);
104 case 0xA2: return this->t->ratings[CompanyID{2}];
105 case 0xA3: return GB(this->t->ratings[CompanyID{2}], 8, 8);
106 case 0xA4: return this->t->ratings[CompanyID{3}];
107 case 0xA5: return GB(this->t->ratings[CompanyID{3}], 8, 8);
108 case 0xA6: return this->t->ratings[CompanyID{4}];
109 case 0xA7: return GB(this->t->ratings[CompanyID{4}], 8, 8);
110 case 0xA8: return this->t->ratings[CompanyID{5}];
111 case 0xA9: return GB(this->t->ratings[CompanyID{5}], 8, 8);
112 case 0xAA: return this->t->ratings[CompanyID{6}];
113 case 0xAB: return GB(this->t->ratings[CompanyID{6}], 8, 8);
114 case 0xAC: return this->t->ratings[CompanyID{7}];
115 case 0xAD: return GB(this->t->ratings[CompanyID{7}], 8, 8);
116 case 0xAE: return this->t->have_ratings.base();
117 case 0xB2: return this->t->statues.base();
118 case 0xB6: return ClampTo<uint16_t>(this->t->cache.num_houses);
119 case 0xB9: return this->t->growth_rate / Ticks::TOWN_GROWTH_TICKS;
120 case 0xBA: return TownHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::production);
121 case 0xBB: return TownHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::production) >> 8;
122 case 0xBC: return TownHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::production);
123 case 0xBD: return TownHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::production) >> 8;
124 case 0xBE: return TownHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::transported);
125 case 0xBF: return TownHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::transported) >> 8;
126 case 0xC0: return TownHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::transported);
127 case 0xC1: return TownHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::transported) >> 8;
128 case 0xC2: return TownHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::production);
129 case 0xC3: return TownHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::production) >> 8;
130 case 0xC4: return TownHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::production);
131 case 0xC5: return TownHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::production) >> 8;
132 case 0xC6: return TownHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::transported);
133 case 0xC7: return TownHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::transported) >> 8;
134 case 0xC8: return TownHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::transported);
135 case 0xC9: return TownHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::transported) >> 8;
136 case 0xCA: return this->t->GetPercentTransported(GetCargoTypeByLabel(CT_PASSENGERS));
137 case 0xCB: return this->t->GetPercentTransported(GetCargoTypeByLabel(CT_MAIL));
138 case 0xCC: return this->t->received[TownAcceptanceEffect::Food].new_act;
139 case 0xCD: return GB(this->t->received[TownAcceptanceEffect::Food].new_act, 8, 8);
140 case 0xCE: return this->t->received[TownAcceptanceEffect::Water].new_act;
141 case 0xCF: return GB(this->t->received[TownAcceptanceEffect::Water].new_act, 8, 8);
142 case 0xD0: return this->t->received[TownAcceptanceEffect::Food].old_act;
143 case 0xD1: return GB(this->t->received[TownAcceptanceEffect::Food].old_act, 8, 8);
144 case 0xD2: return this->t->received[TownAcceptanceEffect::Water].old_act;
145 case 0xD3: return GB(this->t->received[TownAcceptanceEffect::Water].old_act, 8, 8);
146 case 0xD4: return this->t->road_build_months;
147 case 0xD5: return this->t->fund_buildings_months;
148 }
149
150 Debug(grf, 1, "Unhandled town variable 0x{:X}", variable);
151
152 available = false;
153 return UINT_MAX;
154}
155
156/* virtual */ void TownScopeResolver::StorePSA(uint pos, int32_t value)
157{
158 if (this->readonly) return;
159
160 assert(this->t != nullptr);
161 /* We can't store anything if the caller has no #GRFFile. */
162 if (this->ro.grffile == nullptr) return;
163
164 /* Check the persistent storage for the GrfID stored in register 100h. */
165 GrfID grfid = UnflattenNewGRFLabel<GrfID>(this->ro.GetRegister(0x100));
166
167 /* A NewGRF can only write in the persistent storage associated to its own GRFID. */
168 if (grfid == INVALID_GRFID) grfid = this->ro.grffile->grfid;
169 if (grfid != this->ro.grffile->grfid) return;
170
171 /* Check if the storage exists. */
172 for (auto &it : t->psa_list) {
173 if (it->grfid == grfid) {
174 it->StoreValue(pos, value);
175 return;
176 }
177 }
178
179 /* Create a new storage. */
182 psa->StoreValue(pos, value);
183 t->psa_list.push_back(psa);
184}
185
193 : ResolverObject(grffile), town_scope(*this, t, readonly)
194{
195}
196
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
static constexpr CargoLabel CT_PASSENGERS
Available types of cargo Labels may be re-used between different climates.
Definition cargo_type.h:31
@ Food
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:29
@ Water
Cargo behaves water-like.
Definition cargotype.h:28
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
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.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
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:267
@ FakeTowns
Fake town GrfSpecFeature for NewGRF debugging (parent scope).
Definition newgrf.h:104
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 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:124
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.
uint32_t population
Current population of people.
Definition town.h:54
uint32_t num_houses
Amount of houses.
Definition town.h:53
std::array< uint32_t, NUM_HOUSE_ZONES > squared_town_zone_radius
UpdateTownRadius updates this given the house count.
Definition town.h:57
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 transported
Total transported.
Definition town.h:93
uint32_t production
Total produced.
Definition town.h:92
Town data structure.
Definition town.h:64
bool larger_town
if this is a larger town and should grow more quickly
Definition town.h:199
CompanyMask statues
which companies have a statue?
Definition town.h:82
TileIndex xy
town center tile
Definition town.h:65
uint8_t fund_buildings_months
fund buildings program in action?
Definition town.h:196
SuppliedCargoes supplied
Cargo statistics about supplied cargo.
Definition town.h:131
TownLayout layout
town specific road layout
Definition town.h:200
uint16_t grow_counter
counter to count when to grow, value is smaller than or equal to growth_rate
Definition town.h:193
TownFlags flags
See TownFlags.
Definition town.h:78
TownCache cache
Container for all cacheable data.
Definition town.h:67
EnumIndexArray< TransportedCargoStat< uint16_t >, TownAcceptanceEffect, TownAcceptanceEffect::End > received
Cargo statistics about received cargotypes.
Definition town.h:133
CompanyMask have_ratings
which companies have a rating
Definition town.h:85
TypedIndexContainer< std::array< int16_t, MAX_COMPANIES >, CompanyID > ratings
ratings of each company for this town
Definition town.h:89
uint16_t growth_rate
town growth rate
Definition town.h:194
StationList stations_near
NOSAVE: List of nearby stations.
Definition town.h:189
uint8_t road_build_months
fund road reconstruction in action?
Definition town.h:197
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.