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/>.
10 /** @file newgrf_canal.cpp Implementation of NewGRF canals. */
14 #include "newgrf_spritegroup.h"
15 #include "newgrf_canal.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 (TileIndex tile
) : ScopeResolver(), tile(tile
)
31 /* virtual */ uint32
GetRandomBits() const;
32 /* virtual */ uint32
GetVariable(byte variable
, uint32 parameter
, bool *available
) const;
35 /** Resolver object for canals. */
36 struct CanalResolverObject
: public ResolverObject
{
37 CanalScopeResolver canal_scope
;
40 * Canal resolver constructor.
41 * @param feature Which canal feature we want.
42 * @param tile Tile index of canal.
43 * @param callback Callback ID.
44 * @param param1 First parameter (var 10) of the callback.
45 * @param param2 Second parameter (var 18) of the callback.
47 CanalResolverObject(CanalFeature feature
, TileIndex tile
,
48 CallbackID callback
= CBID_NO_CALLBACK
, uint32 param1
= 0, uint32 param2
= 0)
49 : ResolverObject (_water_feature
[feature
].grffile
, callback
, param1
, param2
),
54 /* virtual */ ScopeResolver
*GetScope(VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, byte relative
= 0)
57 case VSG_SCOPE_SELF
: return &this->canal_scope
;
58 default: return ResolverObject::GetScope(scope
, relative
);
62 /* virtual */ const SpriteGroup
*ResolveReal(const RealSpriteGroup
*group
) const;
65 /* virtual */ uint32
CanalScopeResolver::GetRandomBits() const
67 /* Return random bits only for water tiles, not station tiles */
68 return IsWaterTile(this->tile
) ? GetWaterTileRandomBits(this->tile
) : 0;
71 /* virtual */ uint32
CanalScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
76 int z
= GetTileZ(this->tile
);
77 /* Return consistent height within locks */
78 if (IsWaterTile(this->tile
) && GetWaterTileType(this->tile
) == WATER_TILE_LOCK_UPPER
) z
--;
83 case 0x81: return GetTerrainType(this->tile
);
85 /* Dike map: Connectivity info for river and canal tiles
87 * Assignment of bits to directions defined in agreement with
88 * http://projects.tt-forums.net/projects/ttdpatch/repository/revisions/2367/entry/trunk/patches/water.asm#L879
97 (!IsWateredTile(TILE_ADDXY(tile
, -1, 0), DIR_SW
) << 0) // NE
98 + (!IsWateredTile(TILE_ADDXY(tile
, 0, 1), DIR_NW
) << 1) // SE
99 + (!IsWateredTile(TILE_ADDXY(tile
, 1, 0), DIR_NE
) << 2) // SW
100 + (!IsWateredTile(TILE_ADDXY(tile
, 0, -1), DIR_SE
) << 3) // NW
101 + (!IsWateredTile(TILE_ADDXY(tile
, -1, 1), DIR_W
) << 4) // E
102 + (!IsWateredTile(TILE_ADDXY(tile
, 1, 1), DIR_N
) << 5) // S
103 + (!IsWateredTile(TILE_ADDXY(tile
, 1, -1), DIR_E
) << 6) // W
104 + (!IsWateredTile(TILE_ADDXY(tile
, -1, -1), DIR_S
) << 7); // N
108 /* Random data for river or canal tiles, otherwise zero */
109 case 0x83: return IsWaterTile(this->tile
) ? GetWaterTileRandomBits(this->tile
) : 0;
112 DEBUG(grf
, 1, "Unhandled canal variable 0x%02X", variable
);
119 /* virtual */ const SpriteGroup
*CanalResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
121 return group
->get_first();
124 static const SpriteGroup
*CanalResolve (CanalFeature feature
, TileIndex tile
,
125 CallbackID callback
= CBID_NO_CALLBACK
, uint32 param1
= 0, uint32 param2
= 0)
127 CanalResolverObject
object (feature
, tile
, callback
, param1
, param2
);
128 return SpriteGroup::Resolve (_water_feature
[feature
].group
, object
);
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 const SpriteGroup
*group
= CanalResolve (feature
, tile
);
140 if (group
== NULL
) return 0;
142 return group
->GetResult();
146 * Run a specific callback for canals.
147 * @param callback Callback ID.
148 * @param param1 Callback parameter 1.
149 * @param param2 Callback parameter 2.
150 * @param feature For which feature to run the callback.
151 * @param tile Tile index of canal.
152 * @return Callback result or #CALLBACK_FAILED if the callback failed.
154 static uint16
GetCanalCallback(CallbackID callback
, uint32 param1
, uint32 param2
, CanalFeature feature
, TileIndex tile
)
156 CanalResolverObject
object(feature
, tile
, callback
, param1
, param2
);
157 return SpriteGroup::CallbackResult (CanalResolve (feature
, tile
, callback
, param1
, param2
));
161 * Get the new sprite offset for a water tile.
162 * @param tile Tile index of the canal/water tile.
163 * @param feature For which feature to get the new sprite offset.
164 * @param cur_offset Current sprite offset.
165 * @return New sprite offset.
167 uint
GetCanalSpriteOffset(CanalFeature feature
, TileIndex tile
, uint cur_offset
)
169 if (HasBit(_water_feature
[feature
].callback_mask
, CBM_CANAL_SPRITE_OFFSET
)) {
170 uint16 cb
= GetCanalCallback(CBID_CANALS_SPRITE_OFFSET
, cur_offset
, 0, feature
, tile
);
171 if (cb
!= CALLBACK_FAILED
) return cur_offset
+ cb
;