OpenTTD Source  1.11.0-beta2
newgrf_canal.cpp
Go to the documentation of this file.
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  */
7 
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"
17 
18 #include "safeguards.h"
19 
22 
26 
29  {
30  }
31 
32  uint32 GetRandomBits() const override;
33  uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override;
34 };
35 
38  CanalScopeResolver canal_scope;
39  CanalFeature feature;
40 
43 
44  ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
45  {
46  switch (scope) {
47  case VSG_SCOPE_SELF: return &this->canal_scope;
48  default: return ResolverObject::GetScope(scope, relative);
49  }
50  }
51 
52  const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const override;
53 
54  GrfSpecFeature GetFeature() const override;
55  uint32 GetDebugID() const override;
56 };
57 
58 /* virtual */ uint32 CanalScopeResolver::GetRandomBits() const
59 {
60  /* Return random bits only for water tiles, not station tiles */
61  return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
62 }
63 
64 /* virtual */ uint32 CanalScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
65 {
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;
73  }
74 
75  /* Terrain type */
76  case 0x81: return GetTerrainType(this->tile);
77 
78  /* Dike map: Connectivity info for river and canal tiles
79  *
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
87  */
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;
99  }
100 
101  /* Random data for river or canal tiles, otherwise zero */
102  case 0x83: return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
103  }
104 
105  DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
106 
107  *available = false;
108  return UINT_MAX;
109 }
110 
111 
112 /* virtual */ const SpriteGroup *CanalResolverObject::ResolveReal(const RealSpriteGroup *group) const
113 {
114  if (group->num_loaded == 0) return nullptr;
115 
116  return group->loaded[0];
117 }
118 
120 {
121  return GSF_CANALS;
122 }
123 
125 {
126  return this->feature;
127 }
128 
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)
140 {
141  this->root_spritegroup = _water_feature[feature].group;
142 }
143 
151 {
152  CanalResolverObject object(feature, tile);
153  const SpriteGroup *group = object.Resolve();
154  if (group == nullptr) return 0;
155 
156  return group->GetResult();
157 }
158 
168 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
169 {
170  CanalResolverObject object(feature, tile, callback, param1, param2);
171  return object.ResolveCallback();
172 }
173 
181 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
182 {
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;
186  }
187  return cur_offset;
188 }
CanalResolverObject
Resolver object for canals.
Definition: newgrf_canal.cpp:37
LOCK_PART_UPPER
@ LOCK_PART_UPPER
Upper part of a lock.
Definition: water_map.h:67
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
VarSpriteGroupScope
VarSpriteGroupScope
Definition: newgrf_spritegroup.h:100
DIR_SW
@ DIR_SW
Southwest.
Definition: direction_type.h:31
RealSpriteGroup::loaded
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
Definition: newgrf_spritegroup.h:92
ResolverObject::callback_param1
uint32 callback_param1
First parameter (var 10) of the callback.
Definition: newgrf_spritegroup.h:334
WaterFeature
Information about a water feature.
Definition: newgrf_canal.h:22
water.h
DIR_SE
@ DIR_SE
Southeast.
Definition: direction_type.h:29
RealSpriteGroup::num_loaded
byte num_loaded
Number of loaded groups.
Definition: newgrf_spritegroup.h:90
DIR_NW
@ DIR_NW
Northwest.
Definition: direction_type.h:33
CanalScopeResolver::GetVariable
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override
Get a variable value.
Definition: newgrf_canal.cpp:64
CanalResolverObject::CanalResolverObject
CanalResolverObject(CanalFeature feature, TileIndex tile, CallbackID callback=CBID_NO_CALLBACK, uint32 callback_param1=0, uint32 callback_param2=0)
Canal resolver constructor.
Definition: newgrf_canal.cpp:137
CBID_CANALS_SPRITE_OFFSET
@ CBID_CANALS_SPRITE_OFFSET
Add an offset to the default sprite numbers to show another sprite.
Definition: newgrf_callbacks.h:206
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:315
GetCanalCallback
static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
Run a specific callback for canals.
Definition: newgrf_canal.cpp:168
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition: tile_map.cpp:121
GetCanalSpriteOffset
uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
Get the new sprite offset for a water tile.
Definition: newgrf_canal.cpp:181
DIR_W
@ DIR_W
West.
Definition: direction_type.h:32
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition: newgrf_callbacks.h:20
CBM_CANAL_SPRITE_OFFSET
@ CBM_CANAL_SPRITE_OFFSET
Enable add sprite offset callback.
Definition: newgrf_callbacks.h:333
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition: newgrf_spritegroup.h:296
CanalResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0) override
Get a resolver for the scope.
Definition: newgrf_canal.cpp:44
DIR_N
@ DIR_N
North.
Definition: direction_type.h:26
VSG_SCOPE_SELF
@ VSG_SCOPE_SELF
Resolved object itself.
Definition: newgrf_spritegroup.h:103
GetLockPart
static byte GetLockPart(TileIndex t)
Get the part of a lock.
Definition: water_map.h:320
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
CanalResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition: newgrf_canal.cpp:119
ResolverObject::callback_param2
uint32 callback_param2
Second parameter (var 18) of the callback.
Definition: newgrf_spritegroup.h:335
DIR_E
@ DIR_E
East.
Definition: direction_type.h:28
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition: newgrf_callbacks.h:22
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition: newgrf_spritegroup.h:344
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:47
CanalResolverObject::ResolveReal
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const override
Get the real sprites of the grf.
Definition: newgrf_canal.cpp:112
DIR_NE
@ DIR_NE
Northeast.
Definition: direction_type.h:27
IsWateredTile
bool IsWateredTile(TileIndex tile, Direction from)
return true if a tile is a water tile wrt.
Definition: water_cmd.cpp:603
_water_feature
WaterFeature _water_feature[CF_END]
Table of canal 'feature' sprite groups.
Definition: newgrf_canal.cpp:21
safeguards.h
GetCanalSprite
SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
Lookup the base sprite to use for a canal.
Definition: newgrf_canal.cpp:150
DIR_S
@ DIR_S
South.
Definition: direction_type.h:30
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0)
Get a resolver for the scope.
Definition: newgrf_spritegroup.cpp:153
stdafx.h
CanalScopeResolver::GetRandomBits
uint32 GetRandomBits() const override
Get a few random bits.
Definition: newgrf_canal.cpp:58
IsTileType
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:150
CanalScopeResolver::tile
TileIndex tile
Tile containing the canal.
Definition: newgrf_canal.cpp:25
newgrf_spritegroup.h
water_map.h
GrfSpecFeature
GrfSpecFeature
Definition: newgrf.h:66
spritecache.h
SpriteGroup::Resolve
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Definition: newgrf_spritegroup.h:61
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:404
CanalScopeResolver
Scope resolver of a canal tile.
Definition: newgrf_canal.cpp:24
GetWaterTileRandomBits
static byte GetWaterTileRandomBits(TileIndex t)
Get the random bits of the water tile.
Definition: water_map.h:332
RealSpriteGroup
Definition: newgrf_spritegroup.h:79
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition: newgrf_spritegroup.h:333
IsLock
static bool IsLock(TileIndex t)
Is there a lock on a given water tile?
Definition: water_map.h:297
CanalResolverObject::GetDebugID
uint32 GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_canal.cpp:124
TILE_ADDXY
#define TILE_ADDXY(tile, x, y)
Adds a given offset to a tile.
Definition: map_func.h:258
newgrf_canal.h
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition: newgrf_spritegroup.h:297
SpriteGroup
Definition: newgrf_spritegroup.h:57
GetTerrainType
uint32 GetTerrainType(TileIndex tile, TileContext context)
Function used by houses (and soon industries) to get information on type of "terrain" the tile it is ...
Definition: newgrf_commons.cpp:348
debug.h
WaterFeature::group
const SpriteGroup * group
Sprite group to start resolving.
Definition: newgrf_canal.h:23
CanalFeature
CanalFeature
List of different canal 'features'.
Definition: newgrf.h:25