newgrf_town.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "town.h"
00015 #include "newgrf_town.h"
00016 
00023 TownScopeResolver::TownScopeResolver(ResolverObject *ro, Town *t, bool readonly) : ScopeResolver(ro)
00024 {
00025   this->t = t;
00026   this->readonly = readonly;
00027 }
00028 
00029 /* virtual */ uint32 TownScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
00030 {
00031   switch (variable) {
00032     /* Larger towns */
00033     case 0x40:
00034       if (_settings_game.economy.larger_towns == 0) return 2;
00035       if (this->t->larger_town) return 1;
00036       return 0;
00037 
00038     /* Town index */
00039     case 0x41: return this->t->index;
00040 
00041     /* Get a variable from the persistent storage */
00042     case 0x7C: {
00043       /* Check the persistent storage for the GrfID stored in register 100h. */
00044       uint32 grfid = GetRegister(0x100);
00045       if (grfid == 0xFFFFFFFF) {
00046         if (this->ro->grffile == NULL) return 0;
00047         grfid = this->ro->grffile->grfid;
00048       }
00049 
00050       std::list<PersistentStorage *>::iterator iter;
00051       for (iter = this->t->psa_list.begin(); iter != this->t->psa_list.end(); iter++) {
00052         if ((*iter)->grfid == grfid) return (*iter)->GetValue(parameter);
00053       }
00054 
00055       return 0;
00056     }
00057 
00058     /* Town properties */
00059     case 0x80: return this->t->xy;
00060     case 0x81: return GB(this->t->xy, 8, 8);
00061     case 0x82: return ClampToU16(this->t->cache.population);
00062     case 0x83: return GB(ClampToU16(this->t->cache.population), 8, 8);
00063     case 0x8A: return this->t->grow_counter;
00064     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
00065     case 0x93: return 0;
00066     case 0x94: return ClampToU16(this->t->cache.squared_town_zone_radius[0]);
00067     case 0x95: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[0]), 8, 8);
00068     case 0x96: return ClampToU16(this->t->cache.squared_town_zone_radius[1]);
00069     case 0x97: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[1]), 8, 8);
00070     case 0x98: return ClampToU16(this->t->cache.squared_town_zone_radius[2]);
00071     case 0x99: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[2]), 8, 8);
00072     case 0x9A: return ClampToU16(this->t->cache.squared_town_zone_radius[3]);
00073     case 0x9B: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[3]), 8, 8);
00074     case 0x9C: return ClampToU16(this->t->cache.squared_town_zone_radius[4]);
00075     case 0x9D: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[4]), 8, 8);
00076     case 0x9E: return this->t->ratings[0];
00077     case 0x9F: return GB(this->t->ratings[0], 8, 8);
00078     case 0xA0: return this->t->ratings[1];
00079     case 0xA1: return GB(this->t->ratings[1], 8, 8);
00080     case 0xA2: return this->t->ratings[2];
00081     case 0xA3: return GB(this->t->ratings[2], 8, 8);
00082     case 0xA4: return this->t->ratings[3];
00083     case 0xA5: return GB(this->t->ratings[3], 8, 8);
00084     case 0xA6: return this->t->ratings[4];
00085     case 0xA7: return GB(this->t->ratings[4], 8, 8);
00086     case 0xA8: return this->t->ratings[5];
00087     case 0xA9: return GB(this->t->ratings[5], 8, 8);
00088     case 0xAA: return this->t->ratings[6];
00089     case 0xAB: return GB(this->t->ratings[6], 8, 8);
00090     case 0xAC: return this->t->ratings[7];
00091     case 0xAD: return GB(this->t->ratings[7], 8, 8);
00092     case 0xAE: return this->t->have_ratings;
00093     case 0xB2: return this->t->statues;
00094     case 0xB6: return ClampToU16(this->t->cache.num_houses);
00095     case 0xB9: return this->t->growth_rate & (~TOWN_GROW_RATE_CUSTOM);
00096     case 0xBA: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_max);
00097     case 0xBB: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_max), 8, 8);
00098     case 0xBC: return ClampToU16(this->t->supplied[CT_MAIL].new_max);
00099     case 0xBD: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_max), 8, 8);
00100     case 0xBE: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_act);
00101     case 0xBF: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_act), 8, 8);
00102     case 0xC0: return ClampToU16(this->t->supplied[CT_MAIL].new_act);
00103     case 0xC1: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_act), 8, 8);
00104     case 0xC2: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_max);
00105     case 0xC3: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_max), 8, 8);
00106     case 0xC4: return ClampToU16(this->t->supplied[CT_MAIL].old_max);
00107     case 0xC5: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_max), 8, 8);
00108     case 0xC6: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_act);
00109     case 0xC7: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_act), 8, 8);
00110     case 0xC8: return ClampToU16(this->t->supplied[CT_MAIL].old_act);
00111     case 0xC9: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_act), 8, 8);
00112     case 0xCA: return this->t->GetPercentTransported(CT_PASSENGERS);
00113     case 0xCB: return this->t->GetPercentTransported(CT_MAIL);
00114     case 0xCC: return this->t->received[TE_FOOD].new_act;
00115     case 0xCD: return GB(this->t->received[TE_FOOD].new_act, 8, 8);
00116     case 0xCE: return this->t->received[TE_WATER].new_act;
00117     case 0xCF: return GB(this->t->received[TE_WATER].new_act, 8, 8);
00118     case 0xD0: return this->t->received[TE_FOOD].old_act;
00119     case 0xD1: return GB(this->t->received[TE_FOOD].old_act, 8, 8);
00120     case 0xD2: return this->t->received[TE_WATER].old_act;
00121     case 0xD3: return GB(this->t->received[TE_WATER].old_act, 8, 8);
00122     case 0xD4: return this->t->road_build_months;
00123     case 0xD5: return this->t->fund_buildings_months;
00124   }
00125 
00126   DEBUG(grf, 1, "Unhandled town variable 0x%X", variable);
00127 
00128   *available = false;
00129   return UINT_MAX;
00130 }
00131 
00132 /* virtual */ void TownScopeResolver::StorePSA(uint pos, int32 value)
00133 {
00134   if (this->readonly) return;
00135 
00136   assert(this->t != NULL);
00137   /* We can't store anything if the caller has no #GRFFile. */
00138   if (this->ro->grffile == NULL) return;
00139 
00140   /* Check the persistent storage for the GrfID stored in register 100h. */
00141   uint32 grfid = GetRegister(0x100);
00142 
00143   /* A NewGRF can only write in the persistent storage associated to its own GRFID. */
00144   if (grfid == 0xFFFFFFFF) grfid = this->ro->grffile->grfid;
00145   if (grfid != this->ro->grffile->grfid) return;
00146 
00147   /* Check if the storage exists. */
00148   std::list<PersistentStorage *>::iterator iter;
00149   for (iter = t->psa_list.begin(); iter != t->psa_list.end(); iter++) {
00150     if ((*iter)->grfid == grfid) {
00151       (*iter)->StoreValue(pos, value);
00152       return;
00153     }
00154   }
00155 
00156   /* Create a new storage. */
00157   assert(PersistentStorage::CanAllocateItem());
00158   PersistentStorage *psa = new PersistentStorage(grfid);
00159   psa->StoreValue(pos, value);
00160   t->psa_list.push_back(psa);
00161 }
00162 
00169 TownResolverObject::TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
00170     : ResolverObject(grffile), town_scope(this, t, readonly)
00171 {
00172 }
00173