OpenTTD Source: src/newgrf_canal.cpp Source File

OpenTTD Source 13.2.1

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
21 WaterFeature _water_feature[CF_END];
22
24 struct CanalScopeResolver : public ScopeResolver {
25 TileIndex tile;
26
27 CanalScopeResolver(ResolverObject &ro, TileIndex tile)
28 : ScopeResolver(ro), tile(tile)
29 {
30 }
31
32 uint32 GetRandomBits() const override;
33 uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override;
34 };
35
37 struct CanalResolverObject : public ResolverObject {
38 CanalScopeResolver canal_scope;
39 CanalFeature feature;
40
41 CanalResolverObject(CanalFeature feature, TileIndex tile,
42 CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
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 GrfSpecFeature GetFeature() const override;
53 uint32 GetDebugID() const override;
54 };
55
56 /* virtual */ uint32 CanalScopeResolver::GetRandomBits() const
57 {
58 /* Return random bits only for water tiles, not station tiles */
59 return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
60 }
61
62 /* virtual */ uint32 CanalScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
63 {
64 switch (variable) {
65 /* Height of tile */
66 case 0x80: {
67 int z = GetTileZ(this->tile);
68 /* Return consistent height within locks */
69 if (IsTileType(this->tile, MP_WATER) && IsLock(this->tile) && GetLockPart(this->tile) == LOCK_PART_UPPER) z--;
70 return z;
71 }
72
73 /* Terrain type */
74 case 0x81: return GetTerrainType(this->tile);
75
76 /* Dike map: Connectivity info for river and canal tiles
77 *
78 * Assignment of bits to directions defined in agreement with
79 * http://projects.tt-forums.net/projects/ttdpatch/repository/revisions/2367/entry/trunk/patches/water.asm#L879
80 * 7
81 * 3 0
82 * 6 * 4
83 * 2 1
84 * 5
85 */
86 case 0x82: {
87 uint32 connectivity =
88 (!IsWateredTile(TILE_ADDXY(tile, -1, 0), DIR_SW) << 0) // NE
89 + (!IsWateredTile(TILE_ADDXY(tile, 0, 1), DIR_NW) << 1) // SE
90 + (!IsWateredTile(TILE_ADDXY(tile, 1, 0), DIR_NE) << 2) // SW
91 + (!IsWateredTile(TILE_ADDXY(tile, 0, -1), DIR_SE) << 3) // NW
92 + (!IsWateredTile(TILE_ADDXY(tile, -1, 1), DIR_W) << 4) // E
93 + (!IsWateredTile(TILE_ADDXY(tile, 1, 1), DIR_N) << 5) // S
94 + (!IsWateredTile(TILE_ADDXY(tile, 1, -1), DIR_E) << 6) // W
95 + (!IsWateredTile(TILE_ADDXY(tile, -1, -1), DIR_S) << 7); // N
96 return connectivity;
97 }
98
99 /* Random data for river or canal tiles, otherwise zero */
100 case 0x83: return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
101 }
102
103 Debug(grf, 1, "Unhandled canal variable 0x{:02X}", variable);
104
105 *available = false;
106 return UINT_MAX;
107 }
108
109 GrfSpecFeature CanalResolverObject::GetFeature() const
110 {
111 return GSF_CANALS;
112 }
113
114 uint32 CanalResolverObject::GetDebugID() const
115 {
116 return this->feature;
117 }
118
127 CanalResolverObject::CanalResolverObject(CanalFeature feature, TileIndex tile,
128 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
129 : ResolverObject(_water_feature[feature].grffile, callback, callback_param1, callback_param2), canal_scope(*this, tile), feature(feature)
130 {
131 this->root_spritegroup = _water_feature[feature].group;
132 }
133
140 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
141 {
142 CanalResolverObject object(feature, tile);
143 const SpriteGroup *group = object.Resolve();
144 if (group == nullptr) return 0;
145
146 return group->GetResult();
147 }
148
158 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
159 {
160 CanalResolverObject object(feature, tile, callback, param1, param2);
161 return object.ResolveCallback();
162 }
163
171 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
172 {
173 if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
174 uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
175 if (cb != CALLBACK_FAILED) return cur_offset + cb;
176 }
177 return cur_offset;
178 }
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:78
VarSpriteGroupScope
VarSpriteGroupScope
Definition: newgrf_spritegroup.h:97
DIR_SW
@ DIR_SW
Southwest.
Definition: direction_type.h:31
ResolverObject::callback_param1
uint32 callback_param1
First parameter (var 10) of the callback.
Definition: newgrf_spritegroup.h:326
WaterFeature
Information about a water feature.
Definition: newgrf_canal.h:22
water.h
DIR_SE
@ DIR_SE
Southeast.
Definition: direction_type.h:29
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:62
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:127
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:307
GetCanalCallback
static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
Run a specific callback for canals.
Definition: newgrf_canal.cpp:158
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:171
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
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:337
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition: newgrf_spritegroup.h:288
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:100
GetLockPart
static byte GetLockPart(TileIndex t)
Get the part of a lock.
Definition: water_map.h:331
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:109
ResolverObject::callback_param2
uint32 callback_param2
Second parameter (var 18) of the callback.
Definition: newgrf_spritegroup.h:327
DIR_E
@ DIR_E
East.
Definition: direction_type.h:28
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:336
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:54
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:609
_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:140
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:139
stdafx.h
CanalScopeResolver::GetRandomBits
uint32 GetRandomBits() const override
Get a few random bits.
Definition: newgrf_canal.cpp:56
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:408
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:343
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition: newgrf_spritegroup.h:325
IsLock
static bool IsLock(TileIndex t)
Is there a lock on a given water tile?
Definition: water_map.h:308
CanalResolverObject::GetDebugID
uint32 GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_canal.cpp:114
TILE_ADDXY
#define TILE_ADDXY(tile, x, y)
Adds a given offset to a tile.
Definition: map_func.h:258
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
newgrf_canal.h
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition: newgrf_spritegroup.h:289
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