Update: Translations from eints
[openttd-github.git] / src / newgrf_town.cpp
blobd144d56561749ce2cb62e4d092f11d8fa809da08
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 */
8 /** @file newgrf_town.cpp Implementation of the town part of NewGRF houses. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "town.h"
13 #include "newgrf_town.h"
14 #include "timer/timer_game_tick.h"
16 #include "safeguards.h"
18 /* virtual */ uint32_t TownScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
20 if (this->t == nullptr) {
21 available = false;
22 return UINT_MAX;
25 CargoID cid;
26 switch (variable) {
27 /* Larger towns */
28 case 0x40:
29 if (_settings_game.economy.larger_towns == 0) return 2;
30 if (this->t->larger_town) return 1;
31 return 0;
33 /* Town index */
34 case 0x41: return this->t->index;
36 /* Get a variable from the persistent storage */
37 case 0x7C: {
38 /* Check the persistent storage for the GrfID stored in register 100h. */
39 uint32_t grfid = GetRegister(0x100);
40 if (grfid == 0xFFFFFFFF) {
41 if (this->ro.grffile == nullptr) return 0;
42 grfid = this->ro.grffile->grfid;
45 for (auto &it : this->t->psa_list) {
46 if (it->grfid == grfid) return it->GetValue(parameter);
49 return 0;
52 /* Town properties */
53 case 0x80: return this->t->xy.base();
54 case 0x81: return GB(this->t->xy.base(), 8, 8);
55 case 0x82: return ClampTo<uint16_t>(this->t->cache.population);
56 case 0x83: return GB(ClampTo<uint16_t>(this->t->cache.population), 8, 8);
57 case 0x8A: return this->t->grow_counter / Ticks::TOWN_GROWTH_TICKS;
58 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
59 case 0x93: return 0;
60 case 0x94: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_EDGE]);
61 case 0x95: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_EDGE]), 8, 8);
62 case 0x96: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_OUTSKIRT]);
63 case 0x97: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_OUTSKIRT]), 8, 8);
64 case 0x98: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_OUTER_SUBURB]);
65 case 0x99: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_OUTER_SUBURB]), 8, 8);
66 case 0x9A: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_INNER_SUBURB]);
67 case 0x9B: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_INNER_SUBURB]), 8, 8);
68 case 0x9C: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_CENTRE]);
69 case 0x9D: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[HZB_TOWN_CENTRE]), 8, 8);
70 case 0x9E: return this->t->ratings[0];
71 case 0x9F: return GB(this->t->ratings[0], 8, 8);
72 case 0xA0: return this->t->ratings[1];
73 case 0xA1: return GB(this->t->ratings[1], 8, 8);
74 case 0xA2: return this->t->ratings[2];
75 case 0xA3: return GB(this->t->ratings[2], 8, 8);
76 case 0xA4: return this->t->ratings[3];
77 case 0xA5: return GB(this->t->ratings[3], 8, 8);
78 case 0xA6: return this->t->ratings[4];
79 case 0xA7: return GB(this->t->ratings[4], 8, 8);
80 case 0xA8: return this->t->ratings[5];
81 case 0xA9: return GB(this->t->ratings[5], 8, 8);
82 case 0xAA: return this->t->ratings[6];
83 case 0xAB: return GB(this->t->ratings[6], 8, 8);
84 case 0xAC: return this->t->ratings[7];
85 case 0xAD: return GB(this->t->ratings[7], 8, 8);
86 case 0xAE: return this->t->have_ratings;
87 case 0xB2: return this->t->statues;
88 case 0xB6: return ClampTo<uint16_t>(this->t->cache.num_houses);
89 case 0xB9: return this->t->growth_rate / Ticks::TOWN_GROWTH_TICKS;
90 case 0xBA: cid = GetCargoIDByLabel(CT_PASSENGERS); return IsValidCargoID(cid) ? ClampTo<uint16_t>(this->t->supplied[cid].new_max) : 0;
91 case 0xBB: cid = GetCargoIDByLabel(CT_PASSENGERS); return IsValidCargoID(cid) ? GB(ClampTo<uint16_t>(this->t->supplied[cid].new_max), 8, 8) : 0;
92 case 0xBC: cid = GetCargoIDByLabel(CT_MAIL); return IsValidCargoID(cid) ? ClampTo<uint16_t>(this->t->supplied[cid].new_max) : 0;
93 case 0xBD: cid = GetCargoIDByLabel(CT_MAIL); return IsValidCargoID(cid) ? GB(ClampTo<uint16_t>(this->t->supplied[cid].new_max), 8, 8) : 0;
94 case 0xBE: cid = GetCargoIDByLabel(CT_PASSENGERS); return IsValidCargoID(cid) ? ClampTo<uint16_t>(this->t->supplied[cid].new_act) : 0;
95 case 0xBF: cid = GetCargoIDByLabel(CT_PASSENGERS); return IsValidCargoID(cid) ? GB(ClampTo<uint16_t>(this->t->supplied[cid].new_act), 8, 8) : 0;
96 case 0xC0: cid = GetCargoIDByLabel(CT_MAIL); return IsValidCargoID(cid) ? ClampTo<uint16_t>(this->t->supplied[cid].new_act) : 0;
97 case 0xC1: cid = GetCargoIDByLabel(CT_MAIL); return IsValidCargoID(cid) ? GB(ClampTo<uint16_t>(this->t->supplied[cid].new_act), 8, 8) : 0;
98 case 0xC2: cid = GetCargoIDByLabel(CT_PASSENGERS); return IsValidCargoID(cid) ? ClampTo<uint16_t>(this->t->supplied[cid].old_max) : 0;
99 case 0xC3: cid = GetCargoIDByLabel(CT_PASSENGERS); return IsValidCargoID(cid) ? GB(ClampTo<uint16_t>(this->t->supplied[cid].old_max), 8, 8) : 0;
100 case 0xC4: cid = GetCargoIDByLabel(CT_MAIL); return IsValidCargoID(cid) ? ClampTo<uint16_t>(this->t->supplied[cid].old_max) : 0;
101 case 0xC5: cid = GetCargoIDByLabel(CT_MAIL); return IsValidCargoID(cid) ? GB(ClampTo<uint16_t>(this->t->supplied[cid].old_max), 8, 8) : 0;
102 case 0xC6: cid = GetCargoIDByLabel(CT_PASSENGERS); return IsValidCargoID(cid) ? ClampTo<uint16_t>(this->t->supplied[cid].old_act) : 0;
103 case 0xC7: cid = GetCargoIDByLabel(CT_PASSENGERS); return IsValidCargoID(cid) ? GB(ClampTo<uint16_t>(this->t->supplied[cid].old_act), 8, 8) : 0;
104 case 0xC8: cid = GetCargoIDByLabel(CT_MAIL); return IsValidCargoID(cid) ? ClampTo<uint16_t>(this->t->supplied[cid].old_act) : 0;
105 case 0xC9: cid = GetCargoIDByLabel(CT_MAIL); return IsValidCargoID(cid) ? GB(ClampTo<uint16_t>(this->t->supplied[cid].old_act), 8, 8) : 0;
106 case 0xCA: return this->t->GetPercentTransported(GetCargoIDByLabel(CT_PASSENGERS));
107 case 0xCB: return this->t->GetPercentTransported(GetCargoIDByLabel(CT_MAIL));
108 case 0xCC: return this->t->received[TAE_FOOD].new_act;
109 case 0xCD: return GB(this->t->received[TAE_FOOD].new_act, 8, 8);
110 case 0xCE: return this->t->received[TAE_WATER].new_act;
111 case 0xCF: return GB(this->t->received[TAE_WATER].new_act, 8, 8);
112 case 0xD0: return this->t->received[TAE_FOOD].old_act;
113 case 0xD1: return GB(this->t->received[TAE_FOOD].old_act, 8, 8);
114 case 0xD2: return this->t->received[TAE_WATER].old_act;
115 case 0xD3: return GB(this->t->received[TAE_WATER].old_act, 8, 8);
116 case 0xD4: return this->t->road_build_months;
117 case 0xD5: return this->t->fund_buildings_months;
120 Debug(grf, 1, "Unhandled town variable 0x{:X}", variable);
122 available = false;
123 return UINT_MAX;
126 /* virtual */ void TownScopeResolver::StorePSA(uint pos, int32_t value)
128 if (this->readonly) return;
130 assert(this->t != nullptr);
131 /* We can't store anything if the caller has no #GRFFile. */
132 if (this->ro.grffile == nullptr) return;
134 /* Check the persistent storage for the GrfID stored in register 100h. */
135 uint32_t grfid = GetRegister(0x100);
137 /* A NewGRF can only write in the persistent storage associated to its own GRFID. */
138 if (grfid == 0xFFFFFFFF) grfid = this->ro.grffile->grfid;
139 if (grfid != this->ro.grffile->grfid) return;
141 /* Check if the storage exists. */
142 for (auto &it : t->psa_list) {
143 if (it->grfid == grfid) {
144 it->StoreValue(pos, value);
145 return;
149 /* Create a new storage. */
150 assert(PersistentStorage::CanAllocateItem());
151 PersistentStorage *psa = new PersistentStorage(grfid, GSF_FAKE_TOWNS, this->t->xy);
152 psa->StoreValue(pos, value);
153 t->psa_list.push_back(psa);
157 * Resolver for a town.
158 * @param grffile NewGRF file associated with the town.
159 * @param t %Town of the scope.
160 * @param readonly Scope may change persistent storage of the town.
162 TownResolverObject::TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
163 : ResolverObject(grffile), town_scope(*this, t, readonly)