Fix ICU iterators on leading/trailing whitespace
[openttd/fttd.git] / src / newgrf_town.cpp
blobc807e77a846ec6f05916c6098a7743f12f176ecc
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file newgrf_town.cpp Implementation of the town part of NewGRF houses. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "town.h"
15 #include "newgrf_town.h"
17 /**
18 * Resolver of a town scope.
19 * @param ro Surrounding resolver.
20 * @param t %Town of the scope.
21 * @param readonly Scope may change persistent storage of the town.
23 TownScopeResolver::TownScopeResolver(ResolverObject &ro, Town *t, bool readonly) : ScopeResolver(ro)
25 this->t = t;
26 this->readonly = readonly;
29 /* virtual */ uint32 TownScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
31 switch (variable) {
32 /* Larger towns */
33 case 0x40:
34 if (_settings_game.economy.larger_towns == 0) return 2;
35 if (this->t->larger_town) return 1;
36 return 0;
38 /* Town index */
39 case 0x41: return this->t->index;
41 /* Get a variable from the persistent storage */
42 case 0x7C: {
43 /* Check the persistent storage for the GrfID stored in register 100h. */
44 uint32 grfid = GetRegister(0x100);
45 if (grfid == 0xFFFFFFFF) {
46 if (this->ro.grffile == NULL) return 0;
47 grfid = this->ro.grffile->grfid;
50 std::list<PersistentStorage *>::iterator iter;
51 for (iter = this->t->psa_list.begin(); iter != this->t->psa_list.end(); iter++) {
52 if ((*iter)->grfid == grfid) return (*iter)->GetValue(parameter);
55 return 0;
58 /* Town properties */
59 case 0x80: return this->t->xy;
60 case 0x81: return GB(this->t->xy, 8, 8);
61 case 0x82: return ClampToU16(this->t->cache.population);
62 case 0x83: return GB(ClampToU16(this->t->cache.population), 8, 8);
63 case 0x8A: return this->t->grow_counter;
64 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
65 case 0x93: return 0;
66 case 0x94: return ClampToU16(this->t->cache.squared_town_zone_radius[0]);
67 case 0x95: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[0]), 8, 8);
68 case 0x96: return ClampToU16(this->t->cache.squared_town_zone_radius[1]);
69 case 0x97: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[1]), 8, 8);
70 case 0x98: return ClampToU16(this->t->cache.squared_town_zone_radius[2]);
71 case 0x99: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[2]), 8, 8);
72 case 0x9A: return ClampToU16(this->t->cache.squared_town_zone_radius[3]);
73 case 0x9B: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[3]), 8, 8);
74 case 0x9C: return ClampToU16(this->t->cache.squared_town_zone_radius[4]);
75 case 0x9D: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[4]), 8, 8);
76 case 0x9E: return this->t->ratings[0];
77 case 0x9F: return GB(this->t->ratings[0], 8, 8);
78 case 0xA0: return this->t->ratings[1];
79 case 0xA1: return GB(this->t->ratings[1], 8, 8);
80 case 0xA2: return this->t->ratings[2];
81 case 0xA3: return GB(this->t->ratings[2], 8, 8);
82 case 0xA4: return this->t->ratings[3];
83 case 0xA5: return GB(this->t->ratings[3], 8, 8);
84 case 0xA6: return this->t->ratings[4];
85 case 0xA7: return GB(this->t->ratings[4], 8, 8);
86 case 0xA8: return this->t->ratings[5];
87 case 0xA9: return GB(this->t->ratings[5], 8, 8);
88 case 0xAA: return this->t->ratings[6];
89 case 0xAB: return GB(this->t->ratings[6], 8, 8);
90 case 0xAC: return this->t->ratings[7];
91 case 0xAD: return GB(this->t->ratings[7], 8, 8);
92 case 0xAE: return this->t->have_ratings;
93 case 0xB2: return this->t->statues;
94 case 0xB6: return ClampToU16(this->t->cache.num_houses);
95 case 0xB9: return this->t->growth_rate & (~TOWN_GROW_RATE_CUSTOM);
96 case 0xBA: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_max);
97 case 0xBB: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_max), 8, 8);
98 case 0xBC: return ClampToU16(this->t->supplied[CT_MAIL].new_max);
99 case 0xBD: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_max), 8, 8);
100 case 0xBE: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_act);
101 case 0xBF: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_act), 8, 8);
102 case 0xC0: return ClampToU16(this->t->supplied[CT_MAIL].new_act);
103 case 0xC1: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_act), 8, 8);
104 case 0xC2: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_max);
105 case 0xC3: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_max), 8, 8);
106 case 0xC4: return ClampToU16(this->t->supplied[CT_MAIL].old_max);
107 case 0xC5: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_max), 8, 8);
108 case 0xC6: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_act);
109 case 0xC7: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_act), 8, 8);
110 case 0xC8: return ClampToU16(this->t->supplied[CT_MAIL].old_act);
111 case 0xC9: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_act), 8, 8);
112 case 0xCA: return this->t->GetPercentTransported(CT_PASSENGERS);
113 case 0xCB: return this->t->GetPercentTransported(CT_MAIL);
114 case 0xCC: return this->t->received[TE_FOOD].new_act;
115 case 0xCD: return GB(this->t->received[TE_FOOD].new_act, 8, 8);
116 case 0xCE: return this->t->received[TE_WATER].new_act;
117 case 0xCF: return GB(this->t->received[TE_WATER].new_act, 8, 8);
118 case 0xD0: return this->t->received[TE_FOOD].old_act;
119 case 0xD1: return GB(this->t->received[TE_FOOD].old_act, 8, 8);
120 case 0xD2: return this->t->received[TE_WATER].old_act;
121 case 0xD3: return GB(this->t->received[TE_WATER].old_act, 8, 8);
122 case 0xD4: return this->t->road_build_months;
123 case 0xD5: return this->t->fund_buildings_months;
126 DEBUG(grf, 1, "Unhandled town variable 0x%X", variable);
128 *available = false;
129 return UINT_MAX;
132 /* virtual */ void TownScopeResolver::StorePSA(uint pos, int32 value)
134 if (this->readonly) return;
136 assert(this->t != NULL);
137 /* We can't store anything if the caller has no #GRFFile. */
138 if (this->ro.grffile == NULL) return;
140 /* Check the persistent storage for the GrfID stored in register 100h. */
141 uint32 grfid = GetRegister(0x100);
143 /* A NewGRF can only write in the persistent storage associated to its own GRFID. */
144 if (grfid == 0xFFFFFFFF) grfid = this->ro.grffile->grfid;
145 if (grfid != this->ro.grffile->grfid) return;
147 /* Check if the storage exists. */
148 std::list<PersistentStorage *>::iterator iter;
149 for (iter = t->psa_list.begin(); iter != t->psa_list.end(); iter++) {
150 if ((*iter)->grfid == grfid) {
151 (*iter)->StoreValue(pos, value);
152 return;
156 /* Create a new storage. */
157 assert(PersistentStorage::CanAllocateItem());
158 PersistentStorage *psa = new PersistentStorage(grfid, GSF_FAKE_TOWNS, this->t->xy);
159 psa->StoreValue(pos, value);
160 t->psa_list.push_back(psa);
164 * Resolver for a town.
165 * @param grffile NewGRF file associated with the town.
166 * @param t %Town of the scope.
167 * @param readonly Scope may change persistent storage of the town.
169 TownResolverObject::TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
170 : ResolverObject(grffile), town_scope(*this, t, readonly)