OpenTTD Source 20250813-master-g5b5bdd346d
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 <http://www.gnu.org/licenses/>.
6 */
7
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
27/* virtual */ uint32_t TownScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
28{
29 if (this->t == nullptr) {
30 available = false;
31 return UINT_MAX;
32 }
33
34 switch (variable) {
35 /* Larger towns */
36 case 0x40:
37 if (_settings_game.economy.larger_towns == 0) return 2;
38 if (this->t->larger_town) return 1;
39 return 0;
40
41 /* Town index */
42 case 0x41: return this->t->index.base();
43
44 /* Get a variable from the persistent storage */
45 case 0x7C: {
46 /* Check the persistent storage for the GrfID stored in register 100h. */
47 uint32_t grfid = static_cast<uint32_t>(this->ro.GetRegister(0x100));
48 if (grfid == 0xFFFFFFFF) {
49 if (this->ro.grffile == nullptr) return 0;
50 grfid = this->ro.grffile->grfid;
51 }
52
53 for (auto &it : this->t->psa_list) {
54 if (it->grfid == grfid) return it->GetValue(parameter);
55 }
56
57 return 0;
58 }
59
60 /* Town properties */
61 case 0x80: return this->t->xy.base();
62 case 0x81: return GB(this->t->xy.base(), 8, 8);
63 case 0x82: return ClampTo<uint16_t>(this->t->cache.population);
64 case 0x83: return GB(ClampTo<uint16_t>(this->t->cache.population), 8, 8);
65 case 0x8A: return this->t->grow_counter / Ticks::TOWN_GROWTH_TICKS;
66 case 0x92: return this->t->flags; // In original game, 0x92 and 0x93 are really one word. Since flags is a byte, this is to adjust
67 case 0x93: return 0;
68 case 0x94: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownEdge)]);
69 case 0x95: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownEdge)]), 8, 8);
70 case 0x96: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownOutskirt)]);
71 case 0x97: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownOutskirt)]), 8, 8);
72 case 0x98: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownOuterSuburb)]);
73 case 0x99: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownOuterSuburb)]), 8, 8);
74 case 0x9A: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownInnerSuburb)]);
75 case 0x9B: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownInnerSuburb)]), 8, 8);
76 case 0x9C: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownCentre)]);
77 case 0x9D: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[to_underlying(HouseZone::TownCentre)]), 8, 8);
78 case 0x9E: return this->t->ratings[0];
79 case 0x9F: return GB(this->t->ratings[0], 8, 8);
80 case 0xA0: return this->t->ratings[1];
81 case 0xA1: return GB(this->t->ratings[1], 8, 8);
82 case 0xA2: return this->t->ratings[2];
83 case 0xA3: return GB(this->t->ratings[2], 8, 8);
84 case 0xA4: return this->t->ratings[3];
85 case 0xA5: return GB(this->t->ratings[3], 8, 8);
86 case 0xA6: return this->t->ratings[4];
87 case 0xA7: return GB(this->t->ratings[4], 8, 8);
88 case 0xA8: return this->t->ratings[5];
89 case 0xA9: return GB(this->t->ratings[5], 8, 8);
90 case 0xAA: return this->t->ratings[6];
91 case 0xAB: return GB(this->t->ratings[6], 8, 8);
92 case 0xAC: return this->t->ratings[7];
93 case 0xAD: return GB(this->t->ratings[7], 8, 8);
94 case 0xAE: return this->t->have_ratings.base();
95 case 0xB2: return this->t->statues.base();
96 case 0xB6: return ClampTo<uint16_t>(this->t->cache.num_houses);
97 case 0xB9: return this->t->growth_rate / Ticks::TOWN_GROWTH_TICKS;
98 case 0xBA: return TownHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::production);
99 case 0xBB: return TownHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::production) >> 8;
100 case 0xBC: return TownHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::production);
101 case 0xBD: return TownHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::production) >> 8;
102 case 0xBE: return TownHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::transported);
103 case 0xBF: return TownHistoryHelper(this->t, CT_PASSENGERS, THIS_MONTH, &Town::SuppliedHistory::transported) >> 8;
104 case 0xC0: return TownHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::transported);
105 case 0xC1: return TownHistoryHelper(this->t, CT_MAIL, THIS_MONTH, &Town::SuppliedHistory::transported) >> 8;
106 case 0xC2: return TownHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::production);
107 case 0xC3: return TownHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::production) >> 8;
108 case 0xC4: return TownHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::production);
109 case 0xC5: return TownHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::production) >> 8;
110 case 0xC6: return TownHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::transported);
111 case 0xC7: return TownHistoryHelper(this->t, CT_PASSENGERS, LAST_MONTH, &Town::SuppliedHistory::transported) >> 8;
112 case 0xC8: return TownHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::transported);
113 case 0xC9: return TownHistoryHelper(this->t, CT_MAIL, LAST_MONTH, &Town::SuppliedHistory::transported) >> 8;
114 case 0xCA: return this->t->GetPercentTransported(GetCargoTypeByLabel(CT_PASSENGERS));
115 case 0xCB: return this->t->GetPercentTransported(GetCargoTypeByLabel(CT_MAIL));
116 case 0xCC: return this->t->received[TAE_FOOD].new_act;
117 case 0xCD: return GB(this->t->received[TAE_FOOD].new_act, 8, 8);
118 case 0xCE: return this->t->received[TAE_WATER].new_act;
119 case 0xCF: return GB(this->t->received[TAE_WATER].new_act, 8, 8);
120 case 0xD0: return this->t->received[TAE_FOOD].old_act;
121 case 0xD1: return GB(this->t->received[TAE_FOOD].old_act, 8, 8);
122 case 0xD2: return this->t->received[TAE_WATER].old_act;
123 case 0xD3: return GB(this->t->received[TAE_WATER].old_act, 8, 8);
124 case 0xD4: return this->t->road_build_months;
125 case 0xD5: return this->t->fund_buildings_months;
126 }
127
128 Debug(grf, 1, "Unhandled town variable 0x{:X}", variable);
129
130 available = false;
131 return UINT_MAX;
132}
133
134/* virtual */ void TownScopeResolver::StorePSA(uint pos, int32_t value)
135{
136 if (this->readonly) return;
137
138 assert(this->t != nullptr);
139 /* We can't store anything if the caller has no #GRFFile. */
140 if (this->ro.grffile == nullptr) return;
141
142 /* Check the persistent storage for the GrfID stored in register 100h. */
143 uint32_t grfid = static_cast<uint32_t>(this->ro.GetRegister(0x100));
144
145 /* A NewGRF can only write in the persistent storage associated to its own GRFID. */
146 if (grfid == 0xFFFFFFFF) grfid = this->ro.grffile->grfid;
147 if (grfid != this->ro.grffile->grfid) return;
148
149 /* Check if the storage exists. */
150 for (auto &it : t->psa_list) {
151 if (it->grfid == grfid) {
152 it->StoreValue(pos, value);
153 return;
154 }
155 }
156
157 /* Create a new storage. */
159 PersistentStorage *psa = new PersistentStorage(grfid, GSF_FAKE_TOWNS, this->t->xy);
160 psa->StoreValue(pos, value);
161 t->psa_list.push_back(psa);
162}
163
170TownResolverObject::TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
171 : ResolverObject(grffile), town_scope(*this, t, readonly)
172{
173}
174
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static constexpr CargoLabel CT_PASSENGERS
Available types of cargo Labels may be re-used between different climates.
Definition cargo_type.h:31
@ TAE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:29
@ TAE_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:17
@ GSF_FAKE_TOWNS
Fake town GrfSpecFeature for NewGRF debugging (parent scope)
Definition newgrf.h:95
Functions to handle the town part of NewGRF towns.
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.
uint8_t larger_towns
the number of cities to build. These start off larger and grow twice as fast
Dynamic data of a loaded NewGRF.
Definition newgrf.h:115
EconomySettings economy
settings to change the economy
void StoreValue(uint pos, int32_t value)
Stores some value at a given position.
Class for pooled persistent storage of data.
Tindex index
Index of this pool item.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Interface for SpriteGroup-s to access the gamestate.
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
int32_t GetRegister(uint i) const
Gets the value of a so-called newgrf "register".
ResolverObject & ro
Surrounding resolver object.
uint32_t population
Current population of people.
Definition town.h:43
uint32_t num_houses
Amount of houses.
Definition town.h:42
std::array< uint32_t, NUM_HOUSE_ZONES > squared_town_zone_radius
UpdateTownRadius updates this given the house count.
Definition town.h:46
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:80
uint32_t production
Total produced.
Definition town.h:79
Town data structure.
Definition town.h:53
bool larger_town
if this is a larger town and should grow more quickly
Definition town.h:140
CompanyMask statues
which companies have a statue?
Definition town.h:69
TileIndex xy
town center tile
Definition town.h:54
uint8_t fund_buildings_months
fund buildings program in action?
Definition town.h:137
SuppliedCargoes supplied
Cargo statistics about supplied cargo.
Definition town.h:99
uint16_t grow_counter
counter to count when to grow, value is smaller than or equal to growth_rate
Definition town.h:134
uint8_t flags
See TownFlags.
Definition town.h:65
TownCache cache
Container for all cacheable data.
Definition town.h:56
CompanyMask have_ratings
which companies have a rating
Definition town.h:72
TypedIndexContainer< std::array< int16_t, MAX_COMPANIES >, CompanyID > ratings
ratings of each company for this town
Definition town.h:76
uint16_t growth_rate
town growth rate
Definition town.h:135
uint8_t road_build_months
fund road reconstruction in action?
Definition town.h:138
std::array< TransportedCargoStat< uint16_t >, NUM_TAE > received
Cargo statistics about received cargotypes.
Definition town.h:100
Definition of the tick-based game-timer.
Base of the town class.