Rework ChoosePylonPosition to simplify the loop
[openttd/fttd.git] / src / newgrf_town.cpp
bloba29f54fcbc8b71b4a57b940eafaa0ec59fbdb71b
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 grffile GRFFile the resolved SpriteGroup belongs to.
20 * @param t %Town of the scope.
21 * @param readonly Scope may change persistent storage of the town.
23 TownScopeResolver::TownScopeResolver (const GRFFile *grffile, Town *t, bool readonly)
24 : ScopeResolver(), grffile(grffile)
26 this->t = t;
27 this->readonly = readonly;
30 /* virtual */ uint32 TownScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
32 switch (variable) {
33 /* Larger towns */
34 case 0x40:
35 if (_settings_game.economy.larger_towns == 0) return 2;
36 if (this->t->larger_town) return 1;
37 return 0;
39 /* Town index */
40 case 0x41: return this->t->index;
42 /* Get a variable from the persistent storage */
43 case 0x7C: {
44 /* Check the persistent storage for the GrfID stored in register 100h. */
45 uint32 grfid = GetRegister(0x100);
46 if (grfid == 0xFFFFFFFF) {
47 if (this->grffile == NULL) return 0;
48 grfid = this->grffile->grfid;
51 std::list<PersistentStorage *>::iterator iter;
52 for (iter = this->t->psa_list.begin(); iter != this->t->psa_list.end(); iter++) {
53 if ((*iter)->grfid == grfid) return (*iter)->GetValue(parameter);
56 return 0;
59 /* Town properties */
60 case 0x80: return this->t->xy;
61 case 0x81: return GB(this->t->xy, 8, 8);
62 case 0x82: return ClampToU16(this->t->cache.population);
63 case 0x83: return GB(ClampToU16(this->t->cache.population), 8, 8);
64 case 0x8A: return this->t->grow_counter;
65 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
66 case 0x93: return 0;
67 case 0x94: return ClampToU16(this->t->cache.squared_town_zone_radius[0]);
68 case 0x95: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[0]), 8, 8);
69 case 0x96: return ClampToU16(this->t->cache.squared_town_zone_radius[1]);
70 case 0x97: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[1]), 8, 8);
71 case 0x98: return ClampToU16(this->t->cache.squared_town_zone_radius[2]);
72 case 0x99: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[2]), 8, 8);
73 case 0x9A: return ClampToU16(this->t->cache.squared_town_zone_radius[3]);
74 case 0x9B: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[3]), 8, 8);
75 case 0x9C: return ClampToU16(this->t->cache.squared_town_zone_radius[4]);
76 case 0x9D: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[4]), 8, 8);
77 case 0x9E: return this->t->ratings[0];
78 case 0x9F: return GB(this->t->ratings[0], 8, 8);
79 case 0xA0: return this->t->ratings[1];
80 case 0xA1: return GB(this->t->ratings[1], 8, 8);
81 case 0xA2: return this->t->ratings[2];
82 case 0xA3: return GB(this->t->ratings[2], 8, 8);
83 case 0xA4: return this->t->ratings[3];
84 case 0xA5: return GB(this->t->ratings[3], 8, 8);
85 case 0xA6: return this->t->ratings[4];
86 case 0xA7: return GB(this->t->ratings[4], 8, 8);
87 case 0xA8: return this->t->ratings[5];
88 case 0xA9: return GB(this->t->ratings[5], 8, 8);
89 case 0xAA: return this->t->ratings[6];
90 case 0xAB: return GB(this->t->ratings[6], 8, 8);
91 case 0xAC: return this->t->ratings[7];
92 case 0xAD: return GB(this->t->ratings[7], 8, 8);
93 case 0xAE: return this->t->have_ratings;
94 case 0xB2: return this->t->statues;
95 case 0xB6: return ClampToU16(this->t->cache.num_houses);
96 case 0xB9: return this->t->growth_rate & (~TOWN_GROW_RATE_CUSTOM);
97 case 0xBA: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_max);
98 case 0xBB: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_max), 8, 8);
99 case 0xBC: return ClampToU16(this->t->supplied[CT_MAIL].new_max);
100 case 0xBD: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_max), 8, 8);
101 case 0xBE: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_act);
102 case 0xBF: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_act), 8, 8);
103 case 0xC0: return ClampToU16(this->t->supplied[CT_MAIL].new_act);
104 case 0xC1: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_act), 8, 8);
105 case 0xC2: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_max);
106 case 0xC3: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_max), 8, 8);
107 case 0xC4: return ClampToU16(this->t->supplied[CT_MAIL].old_max);
108 case 0xC5: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_max), 8, 8);
109 case 0xC6: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_act);
110 case 0xC7: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_act), 8, 8);
111 case 0xC8: return ClampToU16(this->t->supplied[CT_MAIL].old_act);
112 case 0xC9: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_act), 8, 8);
113 case 0xCA: return this->t->GetPercentTransported(CT_PASSENGERS);
114 case 0xCB: return this->t->GetPercentTransported(CT_MAIL);
115 case 0xCC: return this->t->received[TE_FOOD].new_act;
116 case 0xCD: return GB(this->t->received[TE_FOOD].new_act, 8, 8);
117 case 0xCE: return this->t->received[TE_WATER].new_act;
118 case 0xCF: return GB(this->t->received[TE_WATER].new_act, 8, 8);
119 case 0xD0: return this->t->received[TE_FOOD].old_act;
120 case 0xD1: return GB(this->t->received[TE_FOOD].old_act, 8, 8);
121 case 0xD2: return this->t->received[TE_WATER].old_act;
122 case 0xD3: return GB(this->t->received[TE_WATER].old_act, 8, 8);
123 case 0xD4: return this->t->road_build_months;
124 case 0xD5: return this->t->fund_buildings_months;
127 DEBUG(grf, 1, "Unhandled town variable 0x%X", variable);
129 *available = false;
130 return UINT_MAX;
133 /* virtual */ void TownScopeResolver::StorePSA(uint pos, int32 value)
135 if (this->readonly) return;
137 assert(this->t != NULL);
138 /* We can't store anything if the caller has no #GRFFile. */
139 if (this->grffile == NULL) return;
141 /* Check the persistent storage for the GrfID stored in register 100h. */
142 uint32 grfid = GetRegister(0x100);
144 /* A NewGRF can only write in the persistent storage associated to its own GRFID. */
145 if (grfid == 0xFFFFFFFF) grfid = this->grffile->grfid;
146 if (grfid != this->grffile->grfid) return;
148 /* Check if the storage exists. */
149 std::list<PersistentStorage *>::iterator iter;
150 for (iter = t->psa_list.begin(); iter != t->psa_list.end(); iter++) {
151 if ((*iter)->grfid == grfid) {
152 (*iter)->StoreValue(pos, value);
153 return;
157 /* Create a new storage. */
158 assert(PersistentStorage::CanAllocateItem());
159 PersistentStorage *psa = new PersistentStorage(grfid, GSF_FAKE_TOWNS, this->t->xy);
160 psa->StoreValue(pos, value);
161 t->psa_list.push_back(psa);
164 /* virtual */ uint32 FakeTownScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
166 switch (variable) {
167 /* Town index */
168 case 0x41: return 0xFFFF;
170 case 0x40: case 0x7C: case 0x80: case 0x81: case 0x82: case 0x83: case 0x8A: case 0x92:
171 case 0x93: case 0x94: case 0x95: case 0x96: case 0x97: case 0x98: case 0x99: case 0x9A:
172 case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F: case 0xA0: case 0xA1: case 0xA2:
173 case 0xA3: case 0xA4: case 0xA5: case 0xA6: case 0xA7: case 0xA8: case 0xA9: case 0xAA:
174 case 0xAB: case 0xAC: case 0xAD: case 0xAE: case 0xB2: case 0xB6: case 0xB9: case 0xBA:
175 case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF: case 0xC0: case 0xC1: case 0xC2:
176 case 0xC3: case 0xC4: case 0xC5: case 0xC6: case 0xC7: case 0xC8: case 0xC9: case 0xCA:
177 case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF: case 0xD0: case 0xD1: case 0xD2:
178 case 0xD3: case 0xD4: case 0xD5:
179 return 0;
182 DEBUG(grf, 1, "Unhandled town variable 0x%X", variable);
184 *available = false;
185 return UINT_MAX;
189 * Resolver for a town.
190 * @param grffile NewGRF file associated with the town.
191 * @param t %Town of the scope.
192 * @param readonly Scope may change persistent storage of the town.
194 TownResolverObject::TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
195 : ResolverObject (grffile), town_scope (grffile, t, readonly)