Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / newgrf_canal.cpp
bloba0fe27bc4c5452305f3cda6d7a2c4221ab24c4f6
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_canal.cpp Implementation of NewGRF canals. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "newgrf_spritegroup.h"
15 #include "newgrf_canal.h"
16 #include "water.h"
17 #include "map/water.h"
18 #include "map/slope.h"
20 /** Table of canal 'feature' sprite groups */
21 WaterFeature _water_feature[CF_END];
23 /** Scope resolver of a canal tile. */
24 struct CanalScopeResolver : public ScopeResolver {
25 TileIndex tile; ///< Tile containing the canal.
27 CanalScopeResolver(ResolverObject &ro, TileIndex tile);
29 /* virtual */ uint32 GetRandomBits() const;
30 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
33 /** Resolver object for canals. */
34 struct CanalResolverObject : public ResolverObject {
35 CanalScopeResolver canal_scope;
37 CanalResolverObject(const GRFFile *grffile, TileIndex tile,
38 CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
40 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
42 switch (scope) {
43 case VSG_SCOPE_SELF: return &this->canal_scope;
44 default: return ResolverObject::GetScope(scope, relative);
48 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
51 /* virtual */ uint32 CanalScopeResolver::GetRandomBits() const
53 /* Return random bits only for water tiles, not station tiles */
54 return IsWaterTile(this->tile) ? GetWaterTileRandomBits(this->tile) : 0;
57 /* virtual */ uint32 CanalScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
59 switch (variable) {
60 /* Height of tile */
61 case 0x80: {
62 int z = GetTileZ(this->tile);
63 /* Return consistent height within locks */
64 if (IsWaterTile(this->tile) && IsLock(this->tile) && GetLockPart(this->tile) == LOCK_PART_UPPER) z--;
65 return z;
68 /* Terrain type */
69 case 0x81: return GetTerrainType(this->tile);
71 /* Dike map: Connectivity info for river and canal tiles
73 * Assignment of bits to directions defined in agreement with
74 * http://projects.tt-forums.net/projects/ttdpatch/repository/revisions/2367/entry/trunk/patches/water.asm#L879
75 * 7
76 * 3 0
77 * 6 * 4
78 * 2 1
79 * 5
81 case 0x82: {
82 uint32 connectivity =
83 (!IsWateredTile(TILE_ADDXY(tile, -1, 0), DIR_SW) << 0) // NE
84 + (!IsWateredTile(TILE_ADDXY(tile, 0, 1), DIR_NW) << 1) // SE
85 + (!IsWateredTile(TILE_ADDXY(tile, 1, 0), DIR_NE) << 2) // SW
86 + (!IsWateredTile(TILE_ADDXY(tile, 0, -1), DIR_SE) << 3) // NW
87 + (!IsWateredTile(TILE_ADDXY(tile, -1, 1), DIR_W) << 4) // E
88 + (!IsWateredTile(TILE_ADDXY(tile, 1, 1), DIR_N) << 5) // S
89 + (!IsWateredTile(TILE_ADDXY(tile, 1, -1), DIR_E) << 6) // W
90 + (!IsWateredTile(TILE_ADDXY(tile, -1, -1), DIR_S) << 7); // N
91 return connectivity;
94 /* Random data for river or canal tiles, otherwise zero */
95 case 0x83: return IsWaterTile(this->tile) ? GetWaterTileRandomBits(this->tile) : 0;
98 DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
100 *available = false;
101 return UINT_MAX;
105 /* virtual */ const SpriteGroup *CanalResolverObject::ResolveReal(const RealSpriteGroup *group) const
107 if (group->num_loaded == 0) return NULL;
109 return group->loaded[0];
112 CanalScopeResolver::CanalScopeResolver(ResolverObject &ro, TileIndex tile) : ScopeResolver(ro)
114 this->tile = tile;
118 * Canal resolver constructor.
119 * @param grffile Grf file.
120 * @param tile Tile index of canal.
121 * @param callback Callback ID.
122 * @param callback_param1 First parameter (var 10) of the callback.
123 * @param callback_param2 Second parameter (var 18) of the callback.
125 CanalResolverObject::CanalResolverObject(const GRFFile *grffile, TileIndex tile,
126 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
127 : ResolverObject(grffile, callback, callback_param1, callback_param2), canal_scope(*this, tile)
132 * Lookup the base sprite to use for a canal.
133 * @param feature Which canal feature we want.
134 * @param tile Tile index of canal, if appropriate.
135 * @return Base sprite returned by GRF, or \c 0 if none.
137 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
139 CanalResolverObject object(_water_feature[feature].grffile, tile);
140 const SpriteGroup *group = SpriteGroup::Resolve(_water_feature[feature].group, object);
141 if (group == NULL) return 0;
143 return group->GetResult();
147 * Run a specific callback for canals.
148 * @param callback Callback ID.
149 * @param param1 Callback parameter 1.
150 * @param param2 Callback parameter 2.
151 * @param feature For which feature to run the callback.
152 * @param tile Tile index of canal.
153 * @return Callback result or #CALLBACK_FAILED if the callback failed.
155 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
157 CanalResolverObject object(_water_feature[feature].grffile, tile, callback, param1, param2);
158 const SpriteGroup *group = SpriteGroup::Resolve(_water_feature[feature].group, object);
159 if (group == NULL) return CALLBACK_FAILED;
161 return group->GetCallbackResult();
165 * Get the new sprite offset for a water tile.
166 * @param tile Tile index of the canal/water tile.
167 * @param feature For which feature to get the new sprite offset.
168 * @param cur_offset Current sprite offset.
169 * @return New sprite offset.
171 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
173 if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
174 uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
175 if (cb != CALLBACK_FAILED) return cur_offset + cb;
177 return cur_offset;