2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_town.cpp Implementation of the town part of NewGRF houses. */
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "town.h"
|
2012-11-10 20:38:46 +00:00
|
|
|
#include "newgrf_town.h"
|
2023-08-16 13:01:24 +00:00
|
|
|
#include "timer/timer_game_tick.h"
|
2012-11-10 20:38:46 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2023-09-16 20:20:53 +00:00
|
|
|
/* virtual */ uint32_t TownScopeResolver::GetVariable(byte variable, [[maybe_unused]] uint32_t parameter, bool *available) const
|
2007-03-19 11:27:30 +00:00
|
|
|
{
|
|
|
|
switch (variable) {
|
|
|
|
/* Larger towns */
|
2007-04-21 07:17:14 +00:00
|
|
|
case 0x40:
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.economy.larger_towns == 0) return 2;
|
2012-11-10 20:45:59 +00:00
|
|
|
if (this->t->larger_town) return 1;
|
2007-04-21 07:17:14 +00:00
|
|
|
return 0;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
/* Town index */
|
2012-11-10 20:45:59 +00:00
|
|
|
case 0x41: return this->t->index;
|
2007-03-19 11:27:30 +00:00
|
|
|
|
2011-06-12 20:50:03 +00:00
|
|
|
/* Get a variable from the persistent storage */
|
|
|
|
case 0x7C: {
|
|
|
|
/* Check the persistent storage for the GrfID stored in register 100h. */
|
2023-05-08 17:01:06 +00:00
|
|
|
uint32_t grfid = GetRegister(0x100);
|
2011-06-12 20:50:03 +00:00
|
|
|
if (grfid == 0xFFFFFFFF) {
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->ro.grffile == nullptr) return 0;
|
2013-11-24 14:41:19 +00:00
|
|
|
grfid = this->ro.grffile->grfid;
|
2011-06-12 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 11:09:54 +00:00
|
|
|
for (auto &it : this->t->psa_list) {
|
|
|
|
if (it->grfid == grfid) return it->GetValue(parameter);
|
2011-06-12 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-19 11:27:30 +00:00
|
|
|
/* Town properties */
|
2023-08-15 16:12:05 +00:00
|
|
|
case 0x80: return static_cast<uint32_t>(this->t->xy);
|
2023-08-12 18:14:21 +00:00
|
|
|
case 0x81: return GB(static_cast<uint32_t>(this->t->xy), 8, 8);
|
2023-04-29 07:25:51 +00:00
|
|
|
case 0x82: return ClampTo<uint16_t>(this->t->cache.population);
|
|
|
|
case 0x83: return GB(ClampTo<uint16_t>(this->t->cache.population), 8, 8);
|
2023-08-16 13:01:24 +00:00
|
|
|
case 0x8A: return this->t->grow_counter / Ticks::TOWN_GROWTH_TICKS;
|
2012-11-10 20:45:59 +00:00
|
|
|
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
|
2007-03-19 11:27:30 +00:00
|
|
|
case 0x93: return 0;
|
2023-04-29 07:25:51 +00:00
|
|
|
case 0x94: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[0]);
|
|
|
|
case 0x95: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[0]), 8, 8);
|
|
|
|
case 0x96: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[1]);
|
|
|
|
case 0x97: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[1]), 8, 8);
|
|
|
|
case 0x98: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[2]);
|
|
|
|
case 0x99: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[2]), 8, 8);
|
|
|
|
case 0x9A: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[3]);
|
|
|
|
case 0x9B: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[3]), 8, 8);
|
|
|
|
case 0x9C: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[4]);
|
|
|
|
case 0x9D: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[4]), 8, 8);
|
2012-11-10 20:45:59 +00:00
|
|
|
case 0x9E: return this->t->ratings[0];
|
|
|
|
case 0x9F: return GB(this->t->ratings[0], 8, 8);
|
|
|
|
case 0xA0: return this->t->ratings[1];
|
|
|
|
case 0xA1: return GB(this->t->ratings[1], 8, 8);
|
|
|
|
case 0xA2: return this->t->ratings[2];
|
|
|
|
case 0xA3: return GB(this->t->ratings[2], 8, 8);
|
|
|
|
case 0xA4: return this->t->ratings[3];
|
|
|
|
case 0xA5: return GB(this->t->ratings[3], 8, 8);
|
|
|
|
case 0xA6: return this->t->ratings[4];
|
|
|
|
case 0xA7: return GB(this->t->ratings[4], 8, 8);
|
|
|
|
case 0xA8: return this->t->ratings[5];
|
|
|
|
case 0xA9: return GB(this->t->ratings[5], 8, 8);
|
|
|
|
case 0xAA: return this->t->ratings[6];
|
|
|
|
case 0xAB: return GB(this->t->ratings[6], 8, 8);
|
|
|
|
case 0xAC: return this->t->ratings[7];
|
|
|
|
case 0xAD: return GB(this->t->ratings[7], 8, 8);
|
|
|
|
case 0xAE: return this->t->have_ratings;
|
|
|
|
case 0xB2: return this->t->statues;
|
2023-04-29 07:25:51 +00:00
|
|
|
case 0xB6: return ClampTo<uint16_t>(this->t->cache.num_houses);
|
2023-08-16 13:01:24 +00:00
|
|
|
case 0xB9: return this->t->growth_rate / Ticks::TOWN_GROWTH_TICKS;
|
2023-04-29 07:25:51 +00:00
|
|
|
case 0xBA: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_max);
|
|
|
|
case 0xBB: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_max), 8, 8);
|
|
|
|
case 0xBC: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_max);
|
|
|
|
case 0xBD: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_max), 8, 8);
|
|
|
|
case 0xBE: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_act);
|
|
|
|
case 0xBF: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_act), 8, 8);
|
|
|
|
case 0xC0: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_act);
|
|
|
|
case 0xC1: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_act), 8, 8);
|
|
|
|
case 0xC2: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_max);
|
|
|
|
case 0xC3: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_max), 8, 8);
|
|
|
|
case 0xC4: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_max);
|
|
|
|
case 0xC5: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_max), 8, 8);
|
|
|
|
case 0xC6: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_act);
|
|
|
|
case 0xC7: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_act), 8, 8);
|
|
|
|
case 0xC8: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_act);
|
|
|
|
case 0xC9: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_act), 8, 8);
|
2012-11-10 20:45:59 +00:00
|
|
|
case 0xCA: return this->t->GetPercentTransported(CT_PASSENGERS);
|
|
|
|
case 0xCB: return this->t->GetPercentTransported(CT_MAIL);
|
|
|
|
case 0xCC: return this->t->received[TE_FOOD].new_act;
|
|
|
|
case 0xCD: return GB(this->t->received[TE_FOOD].new_act, 8, 8);
|
|
|
|
case 0xCE: return this->t->received[TE_WATER].new_act;
|
|
|
|
case 0xCF: return GB(this->t->received[TE_WATER].new_act, 8, 8);
|
|
|
|
case 0xD0: return this->t->received[TE_FOOD].old_act;
|
|
|
|
case 0xD1: return GB(this->t->received[TE_FOOD].old_act, 8, 8);
|
|
|
|
case 0xD2: return this->t->received[TE_WATER].old_act;
|
|
|
|
case 0xD3: return GB(this->t->received[TE_WATER].old_act, 8, 8);
|
|
|
|
case 0xD4: return this->t->road_build_months;
|
|
|
|
case 0xD5: return this->t->fund_buildings_months;
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(grf, 1, "Unhandled town variable 0x{:X}", variable);
|
2007-03-19 11:27:30 +00:00
|
|
|
|
|
|
|
*available = false;
|
2009-02-18 09:14:41 +00:00
|
|
|
return UINT_MAX;
|
2007-03-19 11:27:30 +00:00
|
|
|
}
|
2011-06-12 20:50:03 +00:00
|
|
|
|
2023-05-08 17:01:06 +00:00
|
|
|
/* virtual */ void TownScopeResolver::StorePSA(uint pos, int32_t value)
|
2012-11-10 20:38:46 +00:00
|
|
|
{
|
|
|
|
if (this->readonly) return;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(this->t != nullptr);
|
2011-06-12 20:50:03 +00:00
|
|
|
/* We can't store anything if the caller has no #GRFFile. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->ro.grffile == nullptr) return;
|
2011-06-12 20:50:03 +00:00
|
|
|
|
|
|
|
/* Check the persistent storage for the GrfID stored in register 100h. */
|
2023-05-08 17:01:06 +00:00
|
|
|
uint32_t grfid = GetRegister(0x100);
|
2011-06-12 20:50:03 +00:00
|
|
|
|
|
|
|
/* A NewGRF can only write in the persistent storage associated to its own GRFID. */
|
2013-11-24 14:41:19 +00:00
|
|
|
if (grfid == 0xFFFFFFFF) grfid = this->ro.grffile->grfid;
|
|
|
|
if (grfid != this->ro.grffile->grfid) return;
|
2011-06-12 20:50:03 +00:00
|
|
|
|
|
|
|
/* Check if the storage exists. */
|
2023-05-07 11:09:54 +00:00
|
|
|
for (auto &it : t->psa_list) {
|
|
|
|
if (it->grfid == grfid) {
|
|
|
|
it->StoreValue(pos, value);
|
2011-06-12 20:50:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new storage. */
|
2011-06-12 20:53:16 +00:00
|
|
|
assert(PersistentStorage::CanAllocateItem());
|
2013-12-23 18:09:29 +00:00
|
|
|
PersistentStorage *psa = new PersistentStorage(grfid, GSF_FAKE_TOWNS, this->t->xy);
|
2011-06-12 20:50:03 +00:00
|
|
|
psa->StoreValue(pos, value);
|
|
|
|
t->psa_list.push_back(psa);
|
|
|
|
}
|
2012-11-10 20:38:46 +00:00
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Resolver for a town.
|
|
|
|
* @param grffile NewGRF file associated with the town.
|
|
|
|
* @param t %Town of the scope.
|
|
|
|
* @param readonly Scope may change persistent storage of the town.
|
|
|
|
*/
|
2012-11-10 20:38:46 +00:00
|
|
|
TownResolverObject::TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
|
2013-11-24 14:41:19 +00:00
|
|
|
: ResolverObject(grffile), town_scope(*this, t, readonly)
|
2012-11-10 20:38:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|