(svn r27763) -Update from Eints:
[openttd.git] / src / newgrf_canal.cpp
blobcba19cbdbaab01d9e0f3b72f637d94d41fd685e7
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 "water_map.h"
19 #include "safeguards.h"
21 /** Table of canal 'feature' sprite groups */
22 WaterFeature _water_feature[CF_END];
24 /** Scope resolver of a canal tile. */
25 struct CanalScopeResolver : public ScopeResolver {
26 TileIndex tile; ///< Tile containing the canal.
28 CanalScopeResolver(ResolverObject &ro, TileIndex tile);
30 /* virtual */ uint32 GetRandomBits() const;
31 /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
34 /** Resolver object for canals. */
35 struct CanalResolverObject : public ResolverObject {
36 CanalScopeResolver canal_scope;
38 CanalResolverObject(CanalFeature feature, TileIndex tile,
39 CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
41 /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
43 switch (scope) {
44 case VSG_SCOPE_SELF: return &this->canal_scope;
45 default: return ResolverObject::GetScope(scope, relative);
49 /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
52 /* virtual */ uint32 CanalScopeResolver::GetRandomBits() const
54 /* Return random bits only for water tiles, not station tiles */
55 return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
58 /* virtual */ uint32 CanalScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
60 switch (variable) {
61 /* Height of tile */
62 case 0x80: {
63 int z = GetTileZ(this->tile);
64 /* Return consistent height within locks */
65 if (IsTileType(this->tile, MP_WATER) && IsLock(this->tile) && GetLockPart(this->tile) == LOCK_PART_UPPER) z--;
66 return z;
69 /* Terrain type */
70 case 0x81: return GetTerrainType(this->tile);
72 /* Dike map: Connectivity info for river and canal tiles
74 * Assignment of bits to directions defined in agreement with
75 * http://projects.tt-forums.net/projects/ttdpatch/repository/revisions/2367/entry/trunk/patches/water.asm#L879
76 * 7
77 * 3 0
78 * 6 * 4
79 * 2 1
80 * 5
82 case 0x82: {
83 uint32 connectivity =
84 (!IsWateredTile(TILE_ADDXY(tile, -1, 0), DIR_SW) << 0) // NE
85 + (!IsWateredTile(TILE_ADDXY(tile, 0, 1), DIR_NW) << 1) // SE
86 + (!IsWateredTile(TILE_ADDXY(tile, 1, 0), DIR_NE) << 2) // SW
87 + (!IsWateredTile(TILE_ADDXY(tile, 0, -1), DIR_SE) << 3) // NW
88 + (!IsWateredTile(TILE_ADDXY(tile, -1, 1), DIR_W) << 4) // E
89 + (!IsWateredTile(TILE_ADDXY(tile, 1, 1), DIR_N) << 5) // S
90 + (!IsWateredTile(TILE_ADDXY(tile, 1, -1), DIR_E) << 6) // W
91 + (!IsWateredTile(TILE_ADDXY(tile, -1, -1), DIR_S) << 7); // N
92 return connectivity;
95 /* Random data for river or canal tiles, otherwise zero */
96 case 0x83: return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
99 DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
101 *available = false;
102 return UINT_MAX;
106 /* virtual */ const SpriteGroup *CanalResolverObject::ResolveReal(const RealSpriteGroup *group) const
108 if (group->num_loaded == 0) return NULL;
110 return group->loaded[0];
113 CanalScopeResolver::CanalScopeResolver(ResolverObject &ro, TileIndex tile) : ScopeResolver(ro)
115 this->tile = tile;
119 * Canal resolver constructor.
120 * @param feature Which canal feature we want.
121 * @param tile Tile index of canal.
122 * @param callback Callback ID.
123 * @param callback_param1 First parameter (var 10) of the callback.
124 * @param callback_param2 Second parameter (var 18) of the callback.
126 CanalResolverObject::CanalResolverObject(CanalFeature feature, TileIndex tile,
127 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
128 : ResolverObject(_water_feature[feature].grffile, callback, callback_param1, callback_param2), canal_scope(*this, tile)
130 this->root_spritegroup = _water_feature[feature].group;
134 * Lookup the base sprite to use for a canal.
135 * @param feature Which canal feature we want.
136 * @param tile Tile index of canal, if appropriate.
137 * @return Base sprite returned by GRF, or \c 0 if none.
139 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
141 CanalResolverObject object(feature, tile);
142 const SpriteGroup *group = object.Resolve();
143 if (group == NULL) return 0;
145 return group->GetResult();
149 * Run a specific callback for canals.
150 * @param callback Callback ID.
151 * @param param1 Callback parameter 1.
152 * @param param2 Callback parameter 2.
153 * @param feature For which feature to run the callback.
154 * @param tile Tile index of canal.
155 * @return Callback result or #CALLBACK_FAILED if the callback failed.
157 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
159 CanalResolverObject object(feature, tile, callback, param1, param2);
160 return object.ResolveCallback();
164 * Get the new sprite offset for a water tile.
165 * @param tile Tile index of the canal/water tile.
166 * @param feature For which feature to get the new sprite offset.
167 * @param cur_offset Current sprite offset.
168 * @return New sprite offset.
170 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
172 if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
173 uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
174 if (cb != CALLBACK_FAILED) return cur_offset + cb;
176 return cur_offset;