OpenTTD Source: src/newgrf_canal.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
17#include "safeguards.h"
18
20std::array<WaterFeature, CF_END> _water_feature;
21
23 struct CanalScopeResolver : public ScopeResolver {
24 TileIndex tile;
25
26 CanalScopeResolver(ResolverObject &ro, TileIndex tile)
27 : ScopeResolver(ro), tile(tile)
28 {
29 }
30
31 uint32_t GetRandomBits() const override;
32 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
33};
34
36 struct CanalResolverObject : public ResolverObject {
37 CanalScopeResolver canal_scope;
38 CanalFeature feature;
39
40 CanalResolverObject(CanalFeature feature, TileIndex tile,
41 CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0);
42
43 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
44 {
45 switch (scope) {
46 case VSG_SCOPE_SELF: return &this->canal_scope;
47 default: return ResolverObject::GetScope(scope, relative);
48 }
49 }
50
51 GrfSpecFeature GetFeature() const override;
52 uint32_t GetDebugID() const override;
53};
54
55/* virtual */ uint32_t CanalScopeResolver::GetRandomBits() const
56{
57 /* Return random bits only for water tiles, not station tiles */
58 return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
59}
60
61/* virtual */ uint32_t CanalScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
62{
63 switch (variable) {
64 /* Height of tile */
65 case 0x80: {
66 int z = GetTileZ(this->tile);
67 /* Return consistent height within locks */
68 if (IsTileType(this->tile, MP_WATER) && IsLock(this->tile) && GetLockPart(this->tile) == LockPart::Upper) z--;
69 return z;
70 }
71
72 /* Terrain type */
73 case 0x81: return GetTerrainType(this->tile);
74
75 /* Dike map: Connectivity info for river and canal tiles
76 *
77 * Assignment of bits to directions defined in agreement with
78 * http://projects.tt-forums.net/projects/ttdpatch/repository/revisions/2367/entry/trunk/patches/water.asm#L879
79 * 7
80 * 3 0
81 * 6 * 4
82 * 2 1
83 * 5
84 */
85 case 0x82: {
86 uint32_t connectivity =
87 (!IsWateredTile(TileAddXY(tile, -1, 0), DIR_SW) << 0) // NE
88 + (!IsWateredTile(TileAddXY(tile, 0, 1), DIR_NW) << 1) // SE
89 + (!IsWateredTile(TileAddXY(tile, 1, 0), DIR_NE) << 2) // SW
90 + (!IsWateredTile(TileAddXY(tile, 0, -1), DIR_SE) << 3) // NW
91 + (!IsWateredTile(TileAddXY(tile, -1, 1), DIR_W) << 4) // E
92 + (!IsWateredTile(TileAddXY(tile, 1, 1), DIR_N) << 5) // S
93 + (!IsWateredTile(TileAddXY(tile, 1, -1), DIR_E) << 6) // W
94 + (!IsWateredTile(TileAddXY(tile, -1, -1), DIR_S) << 7); // N
95 return connectivity;
96 }
97
98 /* Random data for river or canal tiles, otherwise zero */
99 case 0x83: return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
100 }
101
102 Debug(grf, 1, "Unhandled canal variable 0x{:02X}", variable);
103
104 available = false;
105 return UINT_MAX;
106}
107
108 GrfSpecFeature CanalResolverObject::GetFeature() const
109{
110 return GSF_CANALS;
111}
112
113uint32_t CanalResolverObject::GetDebugID() const
114{
115 return this->feature;
116}
117
126CanalResolverObject::CanalResolverObject(CanalFeature feature, TileIndex tile,
127 CallbackID callback, uint32_t callback_param1, uint32_t callback_param2)
128 : ResolverObject(_water_feature[feature].grffile, callback, callback_param1, callback_param2), canal_scope(*this, tile), feature(feature)
129{
130 this->root_spritegroup = _water_feature[feature].group;
131}
132
139 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
140{
141 CanalResolverObject object(feature, tile);
142 const auto *group = object.Resolve<ResultSpriteGroup>();
143 if (group == nullptr || group->num_sprites == 0) return 0;
144
145 return group->sprite;
146}
147
158 static uint16_t GetCanalCallback(CallbackID callback, uint32_t param1, uint32_t param2, CanalFeature feature, TileIndex tile, std::span<int32_t> regs100 = {})
159{
160 CanalResolverObject object(feature, tile, callback, param1, param2);
161 return object.ResolveCallback(regs100);
162}
163
171uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
172{
173 if (_water_feature[feature].callback_mask.Test(CanalCallbackMask::SpriteOffset)) {
174 uint16_t 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}
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
DIR_SW
@ DIR_SW
Southwest.
Definition direction_type.h:31
DIR_NW
@ DIR_NW
Northwest.
Definition direction_type.h:33
DIR_N
@ DIR_N
North.
Definition direction_type.h:26
DIR_SE
@ DIR_SE
Southeast.
Definition direction_type.h:29
DIR_S
@ DIR_S
South.
Definition direction_type.h:30
DIR_NE
@ DIR_NE
Northeast.
Definition direction_type.h:27
DIR_W
@ DIR_W
West.
Definition direction_type.h:32
DIR_E
@ DIR_E
East.
Definition direction_type.h:28
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
TileAddXY
TileIndex TileAddXY(TileIndex tile, int x, int y)
Adds a given offset to a tile.
Definition map_func.h:483
CanalFeature
CanalFeature
List of different canal 'features'.
Definition newgrf.h:25
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:68
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition newgrf_callbacks.h:22
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition newgrf_callbacks.h:24
CBID_CANALS_SPRITE_OFFSET
@ CBID_CANALS_SPRITE_OFFSET
Add an offset to the default sprite numbers to show another sprite.
Definition newgrf_callbacks.h:208
CanalCallbackMask::SpriteOffset
@ SpriteOffset
Enable add sprite offset callback.
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition newgrf_callbacks.h:439
GetCanalSpriteOffset
uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
Get the new sprite offset for a water tile.
Definition newgrf_canal.cpp:171
_water_feature
std::array< WaterFeature, CF_END > _water_feature
Table of canal 'feature' sprite groups.
Definition newgrf_canal.cpp:20
GetCanalSprite
SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
Lookup the base sprite to use for a canal.
Definition newgrf_canal.cpp:139
GetCanalCallback
static uint16_t GetCanalCallback(CallbackID callback, uint32_t param1, uint32_t param2, CanalFeature feature, TileIndex tile, std::span< int32_t > regs100={})
Run a specific callback for canals.
Definition newgrf_canal.cpp:158
newgrf_canal.h
Handling of NewGRF canals.
GetTerrainType
uint32_t 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:336
newgrf_spritegroup.h
Action 2 handling.
VarSpriteGroupScope
VarSpriteGroupScope
Definition newgrf_spritegroup.h:82
VSG_SCOPE_SELF
@ VSG_SCOPE_SELF
Resolved object itself.
Definition newgrf_spritegroup.h:85
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
CanalResolverObject
Resolver object for canals.
Definition newgrf_canal.cpp:36
CanalResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
Definition newgrf_canal.cpp:43
CanalResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition newgrf_canal.cpp:108
CanalResolverObject::CanalResolverObject
CanalResolverObject(CanalFeature feature, TileIndex tile, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Canal resolver constructor.
Definition newgrf_canal.cpp:126
CanalResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition newgrf_canal.cpp:113
CanalScopeResolver
Scope resolver of a canal tile.
Definition newgrf_canal.cpp:23
CanalScopeResolver::GetVariable
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
Definition newgrf_canal.cpp:61
CanalScopeResolver::GetRandomBits
uint32_t GetRandomBits() const override
Get a few random bits.
Definition newgrf_canal.cpp:55
CanalScopeResolver::tile
TileIndex tile
Tile containing the canal.
Definition newgrf_canal.cpp:24
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition newgrf_spritegroup.h:288
ResolverObject::callback_param2
uint32_t callback_param2
Second parameter (var 18) of the callback.
Definition newgrf_spritegroup.h:342
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition newgrf_spritegroup.h:340
ResolverObject::callback_param1
uint32_t callback_param1
First parameter (var 10) of the callback.
Definition newgrf_spritegroup.h:341
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition newgrf_spritegroup.h:353
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0)
Get a resolver for the scope.
Definition newgrf_spritegroup.cpp:133
ResultSpriteGroup
Definition newgrf_spritegroup.h:217
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition newgrf_spritegroup.h:269
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition newgrf_spritegroup.h:270
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
GetTileZ
int GetTileZ(TileIndex tile)
Get bottom height of the tile.
Definition tile_map.cpp:116
IsTileType
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
MP_WATER
@ MP_WATER
Water tile.
Definition tile_type.h:54
water.h
Functions related to water (management)
IsWateredTile
bool IsWateredTile(TileIndex tile, Direction from)
return true if a tile is a water tile wrt.
Definition water_cmd.cpp:658
water_map.h
Map accessors for water tiles.
LockPart::Upper
@ Upper
Upper part of a lock.
IsLock
bool IsLock(Tile t)
Is there a lock on a given water tile?
Definition water_map.h:305
GetWaterTileRandomBits
uint8_t GetWaterTileRandomBits(Tile t)
Get the random bits of the water tile.
Definition water_map.h:340
GetLockPart
LockPart GetLockPart(Tile t)
Get the part of a lock.
Definition water_map.h:328