2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_canal.cpp Implementation of NewGRF canals. */
|
|
|
|
|
2007-05-06 18:14:33 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "newgrf_spritegroup.h"
|
|
|
|
#include "newgrf_canal.h"
|
2013-05-06 20:48:18 +00:00
|
|
|
#include "water.h"
|
2008-01-20 18:30:53 +00:00
|
|
|
#include "water_map.h"
|
2020-01-26 12:45:51 +00:00
|
|
|
#include "spritecache.h"
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2007-05-06 18:14:33 +00:00
|
|
|
/** Table of canal 'feature' sprite groups */
|
2008-01-21 20:41:04 +00:00
|
|
|
WaterFeature _water_feature[CF_END];
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/** Scope resolver of a canal tile. */
|
2012-11-10 20:39:11 +00:00
|
|
|
struct CanalScopeResolver : public ScopeResolver {
|
2012-11-10 20:46:39 +00:00
|
|
|
TileIndex tile; ///< Tile containing the canal.
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2018-03-11 13:19:41 +00:00
|
|
|
CanalScopeResolver(ResolverObject &ro, TileIndex tile)
|
|
|
|
: ScopeResolver(ro), tile(tile)
|
|
|
|
{
|
|
|
|
}
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2019-03-03 22:25:13 +00:00
|
|
|
uint32 GetRandomBits() const override;
|
2022-01-24 01:17:35 +00:00
|
|
|
uint32 GetVariable(uint16 variable, uint32 parameter, GetVariableExtra *extra) const override;
|
2012-11-10 20:39:11 +00:00
|
|
|
};
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2012-11-10 20:39:11 +00:00
|
|
|
/** Resolver object for canals. */
|
|
|
|
struct CanalResolverObject : public ResolverObject {
|
|
|
|
CanalScopeResolver canal_scope;
|
2020-01-26 12:45:51 +00:00
|
|
|
CanalFeature feature;
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2014-03-03 20:02:31 +00:00
|
|
|
CanalResolverObject(CanalFeature feature, TileIndex tile,
|
2012-11-10 20:39:11 +00:00
|
|
|
CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2023-03-14 17:57:30 +00:00
|
|
|
ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, VarSpriteGroupScopeOffset relative = 0) override
|
2012-11-10 20:39:11 +00:00
|
|
|
{
|
|
|
|
switch (scope) {
|
|
|
|
case VSG_SCOPE_SELF: return &this->canal_scope;
|
2012-11-10 20:45:59 +00:00
|
|
|
default: return ResolverObject::GetScope(scope, relative);
|
2012-11-10 20:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2020-01-26 12:45:51 +00:00
|
|
|
GrfSpecFeature GetFeature() const override;
|
|
|
|
uint32 GetDebugID() const override;
|
2012-11-10 20:39:11 +00:00
|
|
|
};
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2012-11-10 20:39:11 +00:00
|
|
|
/* virtual */ uint32 CanalScopeResolver::GetRandomBits() const
|
2007-05-06 18:14:33 +00:00
|
|
|
{
|
2012-11-10 20:39:11 +00:00
|
|
|
/* Return random bits only for water tiles, not station tiles */
|
|
|
|
return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
|
|
|
|
}
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2022-01-24 01:17:35 +00:00
|
|
|
/* virtual */ uint32 CanalScopeResolver::GetVariable(uint16 variable, uint32 parameter, GetVariableExtra *extra) const
|
2012-11-10 20:39:11 +00:00
|
|
|
{
|
2007-05-06 18:14:33 +00:00
|
|
|
switch (variable) {
|
2009-03-09 13:38:00 +00:00
|
|
|
/* Height of tile */
|
2011-01-09 13:53:05 +00:00
|
|
|
case 0x80: {
|
2012-11-10 20:39:11 +00:00
|
|
|
int z = GetTileZ(this->tile);
|
2011-01-09 13:53:05 +00:00
|
|
|
/* Return consistent height within locks */
|
2012-11-10 20:39:11 +00:00
|
|
|
if (IsTileType(this->tile, MP_WATER) && IsLock(this->tile) && GetLockPart(this->tile) == LOCK_PART_UPPER) z--;
|
2011-01-09 13:53:05 +00:00
|
|
|
return z;
|
|
|
|
}
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2009-03-09 13:38:00 +00:00
|
|
|
/* Terrain type */
|
2012-11-10 20:39:11 +00:00
|
|
|
case 0x81: return GetTerrainType(this->tile);
|
2008-01-20 18:30:53 +00:00
|
|
|
|
2013-05-06 20:48:18 +00:00
|
|
|
/* Dike map: Connectivity info for river and canal tiles
|
|
|
|
*
|
|
|
|
* Assignment of bits to directions defined in agreement with
|
|
|
|
* http://projects.tt-forums.net/projects/ttdpatch/repository/revisions/2367/entry/trunk/patches/water.asm#L879
|
|
|
|
* 7
|
|
|
|
* 3 0
|
|
|
|
* 6 * 4
|
|
|
|
* 2 1
|
|
|
|
* 5
|
|
|
|
*/
|
|
|
|
case 0x82: {
|
|
|
|
uint32 connectivity =
|
|
|
|
(!IsWateredTile(TILE_ADDXY(tile, -1, 0), DIR_SW) << 0) // NE
|
|
|
|
+ (!IsWateredTile(TILE_ADDXY(tile, 0, 1), DIR_NW) << 1) // SE
|
|
|
|
+ (!IsWateredTile(TILE_ADDXY(tile, 1, 0), DIR_NE) << 2) // SW
|
|
|
|
+ (!IsWateredTile(TILE_ADDXY(tile, 0, -1), DIR_SE) << 3) // NW
|
|
|
|
+ (!IsWateredTile(TILE_ADDXY(tile, -1, 1), DIR_W) << 4) // E
|
|
|
|
+ (!IsWateredTile(TILE_ADDXY(tile, 1, 1), DIR_N) << 5) // S
|
|
|
|
+ (!IsWateredTile(TILE_ADDXY(tile, 1, -1), DIR_E) << 6) // W
|
|
|
|
+ (!IsWateredTile(TILE_ADDXY(tile, -1, -1), DIR_S) << 7); // N
|
|
|
|
return connectivity;
|
|
|
|
}
|
|
|
|
|
2009-03-09 13:38:00 +00:00
|
|
|
/* Random data for river or canal tiles, otherwise zero */
|
2012-11-10 20:39:11 +00:00
|
|
|
case 0x83: return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
|
2007-05-06 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 16:43:46 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2020-08-25 00:26:44 +00:00
|
|
|
extra->available = false;
|
2009-02-18 09:14:41 +00:00
|
|
|
return UINT_MAX;
|
2007-05-06 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 12:45:51 +00:00
|
|
|
GrfSpecFeature CanalResolverObject::GetFeature() const
|
|
|
|
{
|
|
|
|
return GSF_CANALS;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 CanalResolverObject::GetDebugID() const
|
|
|
|
{
|
|
|
|
return this->feature;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:39:11 +00:00
|
|
|
/**
|
2012-11-10 20:46:39 +00:00
|
|
|
* Canal resolver constructor.
|
2014-03-03 20:02:31 +00:00
|
|
|
* @param feature Which canal feature we want.
|
2012-11-10 20:46:39 +00:00
|
|
|
* @param tile Tile index of canal.
|
|
|
|
* @param callback Callback ID.
|
|
|
|
* @param callback_param1 First parameter (var 10) of the callback.
|
|
|
|
* @param callback_param2 Second parameter (var 18) of the callback.
|
2012-11-10 20:39:11 +00:00
|
|
|
*/
|
2014-03-03 20:02:31 +00:00
|
|
|
CanalResolverObject::CanalResolverObject(CanalFeature feature, TileIndex tile,
|
2012-11-10 20:39:11 +00:00
|
|
|
CallbackID callback, uint32 callback_param1, uint32 callback_param2)
|
2020-01-26 12:45:51 +00:00
|
|
|
: ResolverObject(_water_feature[feature].grffile, callback, callback_param1, callback_param2), canal_scope(*this, tile), feature(feature)
|
2012-11-10 20:39:11 +00:00
|
|
|
{
|
2014-03-03 20:02:31 +00:00
|
|
|
this->root_spritegroup = _water_feature[feature].group;
|
2012-11-10 20:39:11 +00:00
|
|
|
}
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Lookup the base sprite to use for a canal.
|
|
|
|
* @param feature Which canal feature we want.
|
|
|
|
* @param tile Tile index of canal, if appropriate.
|
2012-11-10 20:39:11 +00:00
|
|
|
* @return Base sprite returned by GRF, or \c 0 if none.
|
2011-01-22 09:53:15 +00:00
|
|
|
*/
|
2007-05-06 18:14:33 +00:00
|
|
|
SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
|
|
|
|
{
|
2014-03-03 20:02:31 +00:00
|
|
|
CanalResolverObject object(feature, tile);
|
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group == nullptr) return 0;
|
2007-05-06 18:14:33 +00:00
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
return group->GetResult();
|
2007-05-06 18:14:33 +00:00
|
|
|
}
|
2010-08-03 17:48:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a specific callback for canals.
|
|
|
|
* @param callback Callback ID.
|
|
|
|
* @param param1 Callback parameter 1.
|
|
|
|
* @param param2 Callback parameter 2.
|
|
|
|
* @param feature For which feature to run the callback.
|
|
|
|
* @param tile Tile index of canal.
|
2012-11-10 20:39:11 +00:00
|
|
|
* @return Callback result or #CALLBACK_FAILED if the callback failed.
|
2010-08-03 17:48:07 +00:00
|
|
|
*/
|
2010-08-29 15:58:43 +00:00
|
|
|
static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
|
2010-08-03 17:48:07 +00:00
|
|
|
{
|
2014-03-03 20:02:31 +00:00
|
|
|
CanalResolverObject object(feature, tile, callback, param1, param2);
|
|
|
|
return object.ResolveCallback();
|
2010-08-03 17:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the new sprite offset for a water tile.
|
|
|
|
* @param tile Tile index of the canal/water tile.
|
|
|
|
* @param feature For which feature to get the new sprite offset.
|
|
|
|
* @param cur_offset Current sprite offset.
|
|
|
|
* @return New sprite offset.
|
|
|
|
*/
|
|
|
|
uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
|
|
|
|
{
|
|
|
|
if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
|
|
|
|
uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
|
|
|
|
if (cb != CALLBACK_FAILED) return cur_offset + cb;
|
|
|
|
}
|
|
|
|
return cur_offset;
|
|
|
|
}
|