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 
8 /** @file tile_cmd.h Generic 'commands' that can be performed on all tiles. */
9 
10 #ifndef TILE_CMD_H
11 #define TILE_CMD_H
12 
13 #include "command_type.h"
14 #include "vehicle_type.h"
15 #include "cargo_type.h"
16 #include "track_type.h"
17 #include "tile_map.h"
18 
19 /** The returned bits of VehicleEnterTile. */
20 enum VehicleEnterTileStatus {
21 	VETS_ENTERED_STATION  = 1, ///< The vehicle entered a station
22 	VETS_ENTERED_WORMHOLE = 2, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel)
23 	VETS_CANNOT_ENTER     = 3, ///< The vehicle cannot enter the tile
24 
25 	/**
26 	 * Shift the VehicleEnterTileStatus this many bits
27 	 * to the right to get the station ID when
28 	 * VETS_ENTERED_STATION is set
29 	 */
30 	VETS_STATION_ID_OFFSET = 8,
31 	VETS_STATION_MASK      = 0xFFFF << VETS_STATION_ID_OFFSET,
32 
33 	/** Bit sets of the above specified bits */
34 	VETSB_CONTINUE         = 0,                          ///< The vehicle can continue normally
35 	VETSB_ENTERED_STATION  = 1 << VETS_ENTERED_STATION,  ///< The vehicle entered a station
36 	VETSB_ENTERED_WORMHOLE = 1 << VETS_ENTERED_WORMHOLE, ///< The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/tunnel)
37 	VETSB_CANNOT_ENTER     = 1 << VETS_CANNOT_ENTER,     ///< The vehicle cannot enter the tile
38 };
39 DECLARE_ENUM_AS_BIT_SET(VehicleEnterTileStatus)
40 
41 /** Tile information, used while rendering the tile */
42 struct TileInfo {
43 	uint x;         ///< X position of the tile in unit coordinates
44 	uint y;         ///< Y position of the tile in unit coordinates
45 	Slope tileh;    ///< Slope of the tile
46 	TileIndex tile; ///< Tile index
47 	int z;          ///< Height
48 };
49 
50 /** Tile description for the 'land area information' tool */
51 struct TileDesc {
52 	StringID str;               ///< Description of the tile
53 	Owner owner[4];             ///< Name of the owner(s)
54 	StringID owner_type[4];     ///< Type of each owner
55 	Date build_date;            ///< Date of construction of tile contents
56 	StringID station_class;     ///< Class of station
57 	StringID station_name;      ///< Type of station within the class
58 	StringID airport_class;     ///< Name of the airport class
59 	StringID airport_name;      ///< Name of the airport
60 	StringID airport_tile_name; ///< Name of the airport tile
61 	const char *grf;            ///< newGRF used for the tile contents
62 	uint64 dparam[2];           ///< Parameters of the \a str string
63 	StringID railtype;          ///< Type of rail on the tile.
64 	uint16 rail_speed;          ///< Speed limit of rail (bridges and track)
65 	StringID roadtype;          ///< Type of road on the tile.
66 	uint16 road_speed;          ///< Speed limit of road (bridges and track)
67 	StringID tramtype;          ///< Type of tram on the tile.
68 	uint16 tram_speed;          ///< Speed limit of tram (bridges and track)
69 };
70 
71 /**
72  * Tile callback function signature for drawing a tile and its contents to the screen
73  * @param ti Information about the tile to draw
74  */
75 typedef void DrawTileProc(TileInfo *ti);
76 typedef int GetSlopeZProc(TileIndex tile, uint x, uint y);
77 typedef CommandCost ClearTileProc(TileIndex tile, DoCommandFlag flags);
78 
79 /**
80  * Tile callback function signature for obtaining cargo acceptance of a tile
81  * @param tile            Tile queried for its accepted cargo
82  * @param acceptance      Storage destination of the cargo acceptance in 1/8
83  * @param always_accepted Bitmask of always accepted cargo types
84  */
85 typedef void AddAcceptedCargoProc(TileIndex tile, CargoArray &acceptance, CargoTypes *always_accepted);
86 
87 /**
88  * Tile callback function signature for obtaining a tile description
89  * @param tile Tile being queried
90  * @param td   Storage pointer for returned tile description
91  */
92 typedef void GetTileDescProc(TileIndex tile, TileDesc *td);
93 
94 /**
95  * Tile callback function signature for getting the possible tracks
96  * that can be taken on a given tile by a given transport.
97  *
98  * The return value contains the existing trackdirs and signal states.
99  *
100  * see track_func.h for usage of TrackStatus.
101  *
102  * @param tile     the tile to get the track status from
103  * @param mode     the mode of transportation
104  * @param sub_mode used to differentiate between different kinds within the mode
105  * @return the track status information
106  */
107 typedef TrackStatus GetTileTrackStatusProc(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side);
108 
109 /**
110  * Tile callback function signature for obtaining the produced cargo of a tile.
111  * @param tile      Tile being queried
112  * @param produced  Destination array for produced cargo
113  */
114 typedef void AddProducedCargoProc(TileIndex tile, CargoArray &produced);
115 typedef bool ClickTileProc(TileIndex tile);
116 typedef void AnimateTileProc(TileIndex tile);
117 typedef void TileLoopProc(TileIndex tile);
118 typedef void ChangeTileOwnerProc(TileIndex tile, Owner old_owner, Owner new_owner);
119 
120 /** @see VehicleEnterTileStatus to see what the return values mean */
121 typedef VehicleEnterTileStatus VehicleEnterTileProc(Vehicle *v, TileIndex tile, int x, int y);
122 typedef Foundation GetFoundationProc(TileIndex tile, Slope tileh);
123 
124 /**
125  * Tile callback function signature of the terraforming callback.
126  *
127  * The function is called when a tile is affected by a terraforming operation.
128  * It has to check if terraforming of the tile is allowed and return extra terraform-cost that depend on the tiletype.
129  * With DC_EXEC in \a flags it has to perform tiletype-specific actions (like clearing land etc., but not the terraforming itself).
130  *
131  * @note The terraforming has not yet taken place. So GetTileZ() and GetTileSlope() refer to the landscape before the terraforming operation.
132  *
133  * @param tile      The involved tile.
134  * @param flags     Command flags passed to the terraform command (DC_EXEC, DC_QUERY_COST, etc.).
135  * @param z_new     TileZ after terraforming.
136  * @param tileh_new Slope after terraforming.
137  * @return Error code or extra cost for terraforming (like clearing land, building foundations, etc., but not the terraforming itself.)
138  */
139 typedef CommandCost TerraformTileProc(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new);
140 
141 /**
142  * Set of callback functions for performing tile operations of a given tile type.
143  * @see TileType
144  */
145 struct TileTypeProcs {
146 	DrawTileProc *draw_tile_proc;                  ///< Called to render the tile and its contents to the screen
147 	GetSlopeZProc *get_slope_z_proc;
148 	ClearTileProc *clear_tile_proc;
149 	AddAcceptedCargoProc *add_accepted_cargo_proc; ///< Adds accepted cargo of the tile to cargo array supplied as parameter
150 	GetTileDescProc *get_tile_desc_proc;           ///< Get a description of a tile (for the 'land area information' tool)
151 	GetTileTrackStatusProc *get_tile_track_status_proc; ///< Get available tracks and status of a tile
152 	ClickTileProc *click_tile_proc;                ///< Called when tile is clicked
153 	AnimateTileProc *animate_tile_proc;
154 	TileLoopProc *tile_loop_proc;
155 	ChangeTileOwnerProc *change_tile_owner_proc;
156 	AddProducedCargoProc *add_produced_cargo_proc; ///< Adds produced cargo of the tile to cargo array supplied as parameter
157 	VehicleEnterTileProc *vehicle_enter_tile_proc; ///< Called when a vehicle enters a tile
158 	GetFoundationProc *get_foundation_proc;
159 	TerraformTileProc *terraform_tile_proc;        ///< Called when a terraforming operation is about to take place
160 };
161 
162 extern const TileTypeProcs * const _tile_type_procs[16];
163 
164 TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side = INVALID_DIAGDIR);
165 VehicleEnterTileStatus VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y);
166 void ChangeTileOwner(TileIndex tile, Owner old_owner, Owner new_owner);
167 void GetTileDesc(TileIndex tile, TileDesc *td);
168 
AddAcceptedCargo(TileIndex tile,CargoArray & acceptance,CargoTypes * always_accepted)169 static inline void AddAcceptedCargo(TileIndex tile, CargoArray &acceptance, CargoTypes *always_accepted)
170 {
171 	AddAcceptedCargoProc *proc = _tile_type_procs[GetTileType(tile)]->add_accepted_cargo_proc;
172 	if (proc == nullptr) return;
173 	CargoTypes dummy = 0; // use dummy bitmask so there don't need to be several 'always_accepted != nullptr' checks
174 	proc(tile, acceptance, always_accepted == nullptr ? &dummy : always_accepted);
175 }
176 
AddProducedCargo(TileIndex tile,CargoArray & produced)177 static inline void AddProducedCargo(TileIndex tile, CargoArray &produced)
178 {
179 	AddProducedCargoProc *proc = _tile_type_procs[GetTileType(tile)]->add_produced_cargo_proc;
180 	if (proc == nullptr) return;
181 	proc(tile, produced);
182 }
183 
AnimateTile(TileIndex tile)184 static inline void AnimateTile(TileIndex tile)
185 {
186 	AnimateTileProc *proc = _tile_type_procs[GetTileType(tile)]->animate_tile_proc;
187 	assert(proc != nullptr);
188 	proc(tile);
189 }
190 
ClickTile(TileIndex tile)191 static inline bool ClickTile(TileIndex tile)
192 {
193 	ClickTileProc *proc = _tile_type_procs[GetTileType(tile)]->click_tile_proc;
194 	if (proc == nullptr) return false;
195 	return proc(tile);
196 }
197 
198 #endif /* TILE_CMD_H */
199