Doc: Clarify comment that SND_05_TRAIN_THROUGH_TUNNEL is only for steam engines ...
[openttd-github.git] / src / newgrf_canal.cpp
blob7295e5551b616ae60bd8c53d5c4500202391d4a2
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file newgrf_canal.cpp Implementation of NewGRF canals. */
10 #include "stdafx.h"
11 #include "debug.h"
12 #include "newgrf_spritegroup.h"
13 #include "newgrf_canal.h"
14 #include "water.h"
15 #include "water_map.h"
16 #include "spritecache.h"
18 #include "safeguards.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)
28 : ScopeResolver(ro), tile(tile)
32 uint32 GetRandomBits() const override;
33 uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override;
36 /** Resolver object for canals. */
37 struct CanalResolverObject : public ResolverObject {
38 CanalScopeResolver canal_scope;
39 CanalFeature feature;
41 CanalResolverObject(CanalFeature feature, TileIndex tile,
42 CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
44 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
46 switch (scope) {
47 case VSG_SCOPE_SELF: return &this->canal_scope;
48 default: return ResolverObject::GetScope(scope, relative);
52 const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
54 GrfSpecFeature GetFeature() const override;
55 uint32 GetDebugID() const override;
58 /* virtual */ uint32 CanalScopeResolver::GetRandomBits() const
60 /* Return random bits only for water tiles, not station tiles */
61 return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
64 /* virtual */ uint32 CanalScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
66 switch (variable) {
67 /* Height of tile */
68 case 0x80: {
69 int z = GetTileZ(this->tile);
70 /* Return consistent height within locks */
71 if (IsTileType(this->tile, MP_WATER) && IsLock(this->tile) && GetLockPart(this->tile) == LOCK_PART_UPPER) z--;
72 return z;
75 /* Terrain type */
76 case 0x81: return GetTerrainType(this->tile);
78 /* Dike map: Connectivity info for river and canal tiles
80 * Assignment of bits to directions defined in agreement with
81 * http://projects.tt-forums.net/projects/ttdpatch/repository/revisions/2367/entry/trunk/patches/water.asm#L879
82 * 7
83 * 3 0
84 * 6 * 4
85 * 2 1
86 * 5
88 case 0x82: {
89 uint32 connectivity =
90 (!IsWateredTile(TILE_ADDXY(tile, -1, 0), DIR_SW) << 0) // NE
91 + (!IsWateredTile(TILE_ADDXY(tile, 0, 1), DIR_NW) << 1) // SE
92 + (!IsWateredTile(TILE_ADDXY(tile, 1, 0), DIR_NE) << 2) // SW
93 + (!IsWateredTile(TILE_ADDXY(tile, 0, -1), DIR_SE) << 3) // NW
94 + (!IsWateredTile(TILE_ADDXY(tile, -1, 1), DIR_W) << 4) // E
95 + (!IsWateredTile(TILE_ADDXY(tile, 1, 1), DIR_N) << 5) // S
96 + (!IsWateredTile(TILE_ADDXY(tile, 1, -1), DIR_E) << 6) // W
97 + (!IsWateredTile(TILE_ADDXY(tile, -1, -1), DIR_S) << 7); // N
98 return connectivity;
101 /* Random data for river or canal tiles, otherwise zero */
102 case 0x83: return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
105 DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
107 *available = false;
108 return UINT_MAX;
112 /* virtual */ const SpriteGroup *CanalResolverObject::ResolveReal(const RealSpriteGroup *group) const
114 if (group->num_loaded == 0) return nullptr;
116 return group->loaded[0];
119 GrfSpecFeature CanalResolverObject::GetFeature() const
121 return GSF_CANALS;
124 uint32 CanalResolverObject::GetDebugID() const
126 return this->feature;
130 * Canal resolver constructor.
131 * @param feature Which canal feature we want.
132 * @param tile Tile index of canal.
133 * @param callback Callback ID.
134 * @param callback_param1 First parameter (var 10) of the callback.
135 * @param callback_param2 Second parameter (var 18) of the callback.
137 CanalResolverObject::CanalResolverObject(CanalFeature feature, TileIndex tile,
138 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
139 : ResolverObject(_water_feature[feature].grffile, callback, callback_param1, callback_param2), canal_scope(*this, tile), feature(feature)
141 this->root_spritegroup = _water_feature[feature].group;
145 * Lookup the base sprite to use for a canal.
146 * @param feature Which canal feature we want.
147 * @param tile Tile index of canal, if appropriate.
148 * @return Base sprite returned by GRF, or \c 0 if none.
150 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
152 CanalResolverObject object(feature, tile);
153 const SpriteGroup *group = object.Resolve();
154 if (group == nullptr) return 0;
156 return group->GetResult();
160 * Run a specific callback for canals.
161 * @param callback Callback ID.
162 * @param param1 Callback parameter 1.
163 * @param param2 Callback parameter 2.
164 * @param feature For which feature to run the callback.
165 * @param tile Tile index of canal.
166 * @return Callback result or #CALLBACK_FAILED if the callback failed.
168 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
170 CanalResolverObject object(feature, tile, callback, param1, param2);
171 return object.ResolveCallback();
175 * Get the new sprite offset for a water tile.
176 * @param tile Tile index of the canal/water tile.
177 * @param feature For which feature to get the new sprite offset.
178 * @param cur_offset Current sprite offset.
179 * @return New sprite offset.
181 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
183 if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
184 uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
185 if (cb != CALLBACK_FAILED) return cur_offset + cb;
187 return cur_offset;