OpenTTD

newgrf_town.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_town.cpp 27984 2018-03-11 13:19:41Z frosch $ */
2
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 */
9
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "town.h"
15 #include "newgrf_town.h"
16
17 #include "safeguards.h"
18
19 /* virtual */ uint32 TownScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
20 {
21 switch (variable) {
22 /* Larger towns */
23 case 0x40:
24 if (_settings_game.economy.larger_towns == 0) return 2;
25 if (this->t->larger_town) return 1;
26 return 0;
27
28 /* Town index */
29 case 0x41: return this->t->index;
30
31 /* Get a variable from the persistent storage */
32 case 0x7C: {
33 /* Check the persistent storage for the GrfID stored in register 100h. */
34 uint32 grfid = GetRegister(0x100);
35 if (grfid == 0xFFFFFFFF) {
36 if (this->ro.grffile == NULL) return 0;
37 grfid = this->ro.grffile->grfid;
38 }
39
40 std::list<PersistentStorage *>::iterator iter;
41 for (iter = this->t->psa_list.begin(); iter != this->t->psa_list.end(); iter++) {
42 if ((*iter)->grfid == grfid) return (*iter)->GetValue(parameter);
43 }
44
45 return 0;
46 }
47
48 /* Town properties */
49 case 0x80: return this->t->xy;
50 case 0x81: return GB(this->t->xy, 8, 8);
51 case 0x82: return ClampToU16(this->t->cache.population);
52 case 0x83: return GB(ClampToU16(this->t->cache.population), 8, 8);
53 case 0x8A: return this->t->grow_counter;
54 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
55 case 0x93: return 0;
56 case 0x94: return ClampToU16(this->t->cache.squared_town_zone_radius[0]);
57 case 0x95: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[0]), 8, 8);
58 case 0x96: return ClampToU16(this->t->cache.squared_town_zone_radius[1]);
59 case 0x97: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[1]), 8, 8);
60 case 0x98: return ClampToU16(this->t->cache.squared_town_zone_radius[2]);
61 case 0x99: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[2]), 8, 8);
62 case 0x9A: return ClampToU16(this->t->cache.squared_town_zone_radius[3]);
63 case 0x9B: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[3]), 8, 8);
64 case 0x9C: return ClampToU16(this->t->cache.squared_town_zone_radius[4]);
65 case 0x9D: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[4]), 8, 8);
66 case 0x9E: return this->t->ratings[0];
67 case 0x9F: return GB(this->t->ratings[0], 8, 8);
68 case 0xA0: return this->t->ratings[1];
69 case 0xA1: return GB(this->t->ratings[1], 8, 8);
70 case 0xA2: return this->t->ratings[2];
71 case 0xA3: return GB(this->t->ratings[2], 8, 8);
72 case 0xA4: return this->t->ratings[3];
73 case 0xA5: return GB(this->t->ratings[3], 8, 8);
74 case 0xA6: return this->t->ratings[4];
75 case 0xA7: return GB(this->t->ratings[4], 8, 8);
76 case 0xA8: return this->t->ratings[5];
77 case 0xA9: return GB(this->t->ratings[5], 8, 8);
78 case 0xAA: return this->t->ratings[6];
79 case 0xAB: return GB(this->t->ratings[6], 8, 8);
80 case 0xAC: return this->t->ratings[7];
81 case 0xAD: return GB(this->t->ratings[7], 8, 8);
82 case 0xAE: return this->t->have_ratings;
83 case 0xB2: return this->t->statues;
84 case 0xB6: return ClampToU16(this->t->cache.num_houses);
85 case 0xB9: return this->t->growth_rate & (~TOWN_GROW_RATE_CUSTOM);
86 case 0xBA: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_max);
87 case 0xBB: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_max), 8, 8);
88 case 0xBC: return ClampToU16(this->t->supplied[CT_MAIL].new_max);
89 case 0xBD: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_max), 8, 8);
90 case 0xBE: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_act);
91 case 0xBF: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_act), 8, 8);
92 case 0xC0: return ClampToU16(this->t->supplied[CT_MAIL].new_act);
93 case 0xC1: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_act), 8, 8);
94 case 0xC2: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_max);
95 case 0xC3: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_max), 8, 8);
96 case 0xC4: return ClampToU16(this->t->supplied[CT_MAIL].old_max);
97 case 0xC5: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_max), 8, 8);
98 case 0xC6: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_act);
99 case 0xC7: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_act), 8, 8);
100 case 0xC8: return ClampToU16(this->t->supplied[CT_MAIL].old_act);
101 case 0xC9: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_act), 8, 8);
102 case 0xCA: return this->t->GetPercentTransported(CT_PASSENGERS);
103 case 0xCB: return this->t->GetPercentTransported(CT_MAIL);
104 case 0xCC: return this->t->received[TE_FOOD].new_act;
105 case 0xCD: return GB(this->t->received[TE_FOOD].new_act, 8, 8);
106 case 0xCE: return this->t->received[TE_WATER].new_act;
107 case 0xCF: return GB(this->t->received[TE_WATER].new_act, 8, 8);
108 case 0xD0: return this->t->received[TE_FOOD].old_act;
109 case 0xD1: return GB(this->t->received[TE_FOOD].old_act, 8, 8);
110 case 0xD2: return this->t->received[TE_WATER].old_act;
111 case 0xD3: return GB(this->t->received[TE_WATER].old_act, 8, 8);
112 case 0xD4: return this->t->road_build_months;
113 case 0xD5: return this->t->fund_buildings_months;
114 }
115
116 DEBUG(grf, 1, "Unhandled town variable 0x%X", variable);
117
118 *available = false;
119 return UINT_MAX;
120 }
121
122 /* virtual */ void TownScopeResolver::StorePSA(uint pos, int32 value)
123 {
124 if (this->readonly) return;
125
126 assert(this->t != NULL);
127 /* We can't store anything if the caller has no #GRFFile. */
128 if (this->ro.grffile == NULL) return;
129
130 /* Check the persistent storage for the GrfID stored in register 100h. */
131 uint32 grfid = GetRegister(0x100);
132
133 /* A NewGRF can only write in the persistent storage associated to its own GRFID. */
134 if (grfid == 0xFFFFFFFF) grfid = this->ro.grffile->grfid;
135 if (grfid != this->ro.grffile->grfid) return;
136
137 /* Check if the storage exists. */
138 std::list<PersistentStorage *>::iterator iter;
139 for (iter = t->psa_list.begin(); iter != t->psa_list.end(); iter++) {
140 if ((*iter)->grfid == grfid) {
141 (*iter)->StoreValue(pos, value);
142 return;
143 }
144 }
145
146 /* Create a new storage. */
147 assert(PersistentStorage::CanAllocateItem());
148 PersistentStorage *psa = new PersistentStorage(grfid, GSF_FAKE_TOWNS, this->t->xy);
149 psa->StoreValue(pos, value);
150 t->psa_list.push_back(psa);
151 }
152
159 TownResolverObject::TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
160 : ResolverObject(grffile), town_scope(*this, t, readonly)
161 {
162 }
163
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
Town::received
TransportedCargoStat< uint16 > received[NUM_TE]
Cargo statistics about received cargotypes.
Definition: town.h:80
Town::statues
CompanyMask statues
which companies have a statue?
Definition: town.h:70
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition: newgrf_spritegroup.h:292
GameSettings::economy
EconomySettings economy
settings to change the economy
Definition: settings_type.h:553
TownCache::squared_town_zone_radius
uint32 squared_town_zone_radius[HZB_END]
UpdateTownRadius updates this given the house count.
Definition: town.h:50
Town::fund_buildings_months
byte fund_buildings_months
fund buildings program in action?
Definition: town.h:97
debug.h
Functions related to debugging.
Town::flags
byte flags
See TownFlags.
Definition: town.h:66
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:310
TownScopeResolver::readonly
bool readonly
When set, persistent storage of the town is read-only,.
Definition: newgrf_town.h:26
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
TE_WATER
Cargo behaves water-like.
Definition: cargotype.h:31
TownScopeResolver::GetVariable
virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
Definition: newgrf_town.cpp:19
TownCache::population
uint32 population
Current population of people.
Definition: town.h:47
TransportedCargoStat::new_act
Tstorage new_act
Actually transported this month.
Definition: town_type.h:116
GSF_FAKE_TOWNS
Fake town GrfSpecFeature for NewGRF debugging (parent scope)
Definition: newgrf.h:88
GetRegister
static uint32 GetRegister(uint i)
Gets the value of a so-called newgrf "register".
Definition: newgrf_spritegroup.h:30
Town::xy
TileIndex xy
town center tile
Definition: town.h:56
Town::ratings
int16 ratings[MAX_COMPANIES]
ratings of each company for this town
Definition: town.h:77
PersistentStorageArray::StoreValue
void StoreValue(uint pos, int32 value)
Stores some value at a given position.
Definition: newgrf_storage.h:97
TOWN_GROW_RATE_CUSTOM
static const uint16 TOWN_GROW_RATE_CUSTOM
If this mask is applied to Town::growth_rate, the grow_counter will not be calculated by the system (...
Definition: town.h:38
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
safeguards.h
A number of safeguards to prevent using unsafe methods.
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:223
TransportedCargoStat::new_max
Tstorage new_max
Maximum amount this month.
Definition: town_type.h:114
TownScopeResolver::t
Town * t
Town of the scope.
Definition: newgrf_town.h:25
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
ResolverObject::grffile
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
Definition: newgrf_spritegroup.h:338
Town::growth_rate
uint16 growth_rate
town growth rate
Definition: town.h:95
TransportedCargoStat::old_max
Tstorage old_max
Maximum amount last month.
Definition: town_type.h:113
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:34
Town::cache
TownCache cache
Container for all cacheable data.
Definition: town.h:58
EconomySettings::larger_towns
uint8 larger_towns
the number of cities to build. These start off larger and grow twice as fast
Definition: settings_type.h:483
Town
Town data structure.
Definition: town.h:55
Pool::PoolItem<&_persistent_storage_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
Town::larger_town
bool larger_town
if this is a larger town and should grow more quickly
Definition: town.h:100
TE_FOOD
Cargo behaves food/fizzy-drinks-like.
Definition: cargotype.h:32
TownScopeResolver::StorePSA
virtual void StorePSA(uint reg, int32 value)
Store a value into the persistent storage area (PSA).
Definition: newgrf_town.cpp:122
town.h
Base of the town class.
newgrf_town.h
Functions to handle the town part of NewGRF towns.
Town::supplied
TransportedCargoStat< uint32 > supplied[NUM_CARGO]
Cargo statistics about supplied cargo.
Definition: town.h:79
TownCache::num_houses
uint32 num_houses
Amount of houses.
Definition: town.h:46
Town::road_build_months
byte road_build_months
fund road reconstruction in action?
Definition: town.h:98
Town::grow_counter
uint16 grow_counter
counter to count when to grow, value is smaller than or equal to growth_rate
Definition: town.h:94
ClampToU16
static uint16 ClampToU16(const uint64 a)
Reduce an unsigned 64-bit int to an unsigned 16-bit one.
Definition: math_func.hpp:215
TransportedCargoStat::old_act
Tstorage old_act
Actually transported last month.
Definition: town_type.h:115
Town::have_ratings
CompanyMask have_ratings
which companies have a rating
Definition: town.h:73
TownResolverObject::TownResolverObject
TownResolverObject(const struct GRFFile *grffile, Town *t, bool readonly)
Resolver for a town.
Definition: newgrf_town.cpp:159
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:104