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/>.
8 /** @file clear_cmd.cpp Commands related to clear tiles. */
11 #include "clear_map.h"
12 #include "command_func.h"
13 #include "landscape.h"
15 #include "viewport_func.h"
17 #include "core/random_func.hpp"
18 #include "newgrf_generic.h"
19 #include "landscape_cmd.h"
21 #include "table/strings.h"
22 #include "table/sprites.h"
23 #include "table/clear_land.h"
25 #include "safeguards.h"
27 static CommandCost
ClearTile_Clear(TileIndex tile
, DoCommandFlag flags
)
29 static const Price clear_price_table
[] = {
37 CommandCost
price(EXPENSES_CONSTRUCTION
);
39 if (!IsClearGround(tile
, CLEAR_GRASS
) || GetClearDensity(tile
) != 0) {
40 price
.AddCost(_price
[clear_price_table
[GetClearGround(tile
)]]);
43 if (flags
& DC_EXEC
) DoClearSquare(tile
);
48 void DrawClearLandTile(const TileInfo
*ti
, byte set
)
50 DrawGroundSprite(SPR_FLAT_BARE_LAND
+ SlopeToSpriteOffset(ti
->tileh
) + set
* 19, PAL_NONE
);
53 void DrawHillyLandTile(const TileInfo
*ti
)
55 if (ti
->tileh
!= SLOPE_FLAT
) {
56 DrawGroundSprite(SPR_FLAT_ROUGH_LAND
+ SlopeToSpriteOffset(ti
->tileh
), PAL_NONE
);
58 DrawGroundSprite(_landscape_clear_sprites_rough
[GB(TileHash(ti
->x
, ti
->y
), 0, 3)], PAL_NONE
);
62 static void DrawClearLandFence(const TileInfo
*ti
)
64 /* combine fences into one sprite object */
67 int maxz
= GetSlopeMaxPixelZ(ti
->tileh
);
69 uint fence_nw
= GetFence(ti
->tile
, DIAGDIR_NW
);
71 int z
= GetSlopePixelZInCorner(ti
->tileh
, CORNER_W
);
72 SpriteID sprite
= _clear_land_fence_sprites
[fence_nw
- 1] + _fence_mod_by_tileh_nw
[ti
->tileh
];
73 AddSortableSpriteToDraw(sprite
, PAL_NONE
, ti
->x
, ti
->y
- 15, 16, 31, maxz
- z
+ 4, ti
->z
+ z
, false, 0, 15, -z
);
76 uint fence_ne
= GetFence(ti
->tile
, DIAGDIR_NE
);
78 int z
= GetSlopePixelZInCorner(ti
->tileh
, CORNER_E
);
79 SpriteID sprite
= _clear_land_fence_sprites
[fence_ne
- 1] + _fence_mod_by_tileh_ne
[ti
->tileh
];
80 AddSortableSpriteToDraw(sprite
, PAL_NONE
, ti
->x
- 15, ti
->y
, 31, 16, maxz
- z
+ 4, ti
->z
+ z
, false, 15, 0, -z
);
83 uint fence_sw
= GetFence(ti
->tile
, DIAGDIR_SW
);
84 uint fence_se
= GetFence(ti
->tile
, DIAGDIR_SE
);
86 if (fence_sw
!= 0 || fence_se
!= 0) {
87 int z
= GetSlopePixelZInCorner(ti
->tileh
, CORNER_S
);
90 SpriteID sprite
= _clear_land_fence_sprites
[fence_sw
- 1] + _fence_mod_by_tileh_sw
[ti
->tileh
];
91 AddSortableSpriteToDraw(sprite
, PAL_NONE
, ti
->x
, ti
->y
, 16, 16, maxz
- z
+ 4, ti
->z
+ z
, false, 0, 0, -z
);
95 SpriteID sprite
= _clear_land_fence_sprites
[fence_se
- 1] + _fence_mod_by_tileh_se
[ti
->tileh
];
96 AddSortableSpriteToDraw(sprite
, PAL_NONE
, ti
->x
, ti
->y
, 16, 16, maxz
- z
+ 4, ti
->z
+ z
, false, 0, 0, -z
);
102 static void DrawTile_Clear(TileInfo
*ti
)
104 switch (GetClearGround(ti
->tile
)) {
106 DrawClearLandTile(ti
, GetClearDensity(ti
->tile
));
110 DrawHillyLandTile(ti
);
114 DrawGroundSprite((HasGrfMiscBit(GMB_SECOND_ROCKY_TILE_SET
) && (TileHash(ti
->x
, ti
->y
) & 1) ? SPR_FLAT_ROCKY_LAND_2
: SPR_FLAT_ROCKY_LAND_1
) + SlopeToSpriteOffset(ti
->tileh
), PAL_NONE
);
118 DrawGroundSprite(_clear_land_sprites_farmland
[GetFieldType(ti
->tile
)] + SlopeToSpriteOffset(ti
->tileh
), PAL_NONE
);
119 DrawClearLandFence(ti
);
124 DrawGroundSprite(_clear_land_sprites_snow_desert
[GetClearDensity(ti
->tile
)] + SlopeToSpriteOffset(ti
->tileh
), PAL_NONE
);
128 DrawBridgeMiddle(ti
);
131 static int GetSlopePixelZ_Clear(TileIndex tile
, uint x
, uint y
)
134 Slope tileh
= GetTilePixelSlope(tile
, &z
);
136 return z
+ GetPartialPixelZ(x
& 0xF, y
& 0xF, tileh
);
139 static Foundation
GetFoundation_Clear(TileIndex tile
, Slope tileh
)
141 return FOUNDATION_NONE
;
144 static void UpdateFences(TileIndex tile
)
146 assert(IsTileType(tile
, MP_CLEAR
) && IsClearGround(tile
, CLEAR_FIELDS
));
149 bool neighbour
= (IsTileType(TILE_ADDXY(tile
, 1, 0), MP_CLEAR
) && IsClearGround(TILE_ADDXY(tile
, 1, 0), CLEAR_FIELDS
));
150 if (!neighbour
&& GetFence(tile
, DIAGDIR_SW
) == 0) {
151 SetFence(tile
, DIAGDIR_SW
, 3);
155 neighbour
= (IsTileType(TILE_ADDXY(tile
, 0, 1), MP_CLEAR
) && IsClearGround(TILE_ADDXY(tile
, 0, 1), CLEAR_FIELDS
));
156 if (!neighbour
&& GetFence(tile
, DIAGDIR_SE
) == 0) {
157 SetFence(tile
, DIAGDIR_SE
, 3);
161 neighbour
= (IsTileType(TILE_ADDXY(tile
, -1, 0), MP_CLEAR
) && IsClearGround(TILE_ADDXY(tile
, -1, 0), CLEAR_FIELDS
));
162 if (!neighbour
&& GetFence(tile
, DIAGDIR_NE
) == 0) {
163 SetFence(tile
, DIAGDIR_NE
, 3);
167 neighbour
= (IsTileType(TILE_ADDXY(tile
, 0, -1), MP_CLEAR
) && IsClearGround(TILE_ADDXY(tile
, 0, -1), CLEAR_FIELDS
));
168 if (!neighbour
&& GetFence(tile
, DIAGDIR_NW
) == 0) {
169 SetFence(tile
, DIAGDIR_NW
, 3);
173 if (dirty
) MarkTileDirtyByTile(tile
);
177 /** Convert to or from snowy tiles. */
178 static void TileLoopClearAlps(TileIndex tile
)
180 int k
= GetTileZ(tile
) - GetSnowLine() + 1;
183 /* Below the snow line, do nothing if no snow. */
184 if (!IsSnowTile(tile
)) return;
186 /* At or above the snow line, make snow tile if needed. */
187 if (!IsSnowTile(tile
)) {
189 MarkTileDirtyByTile(tile
);
193 /* Update snow density. */
194 uint current_density
= GetClearDensity(tile
);
195 uint req_density
= (k
< 0) ? 0u : std::min
<uint
>(k
, 3u);
197 if (current_density
< req_density
) {
198 AddClearDensity(tile
, 1);
199 } else if (current_density
> req_density
) {
200 AddClearDensity(tile
, -1);
202 /* Density at the required level. */
206 MarkTileDirtyByTile(tile
);
210 * Tests if at least one surrounding tile is non-desert
211 * @param tile tile to check
212 * @return does this tile have at least one non-desert tile around?
214 static inline bool NeighbourIsNormal(TileIndex tile
)
216 for (DiagDirection dir
= DIAGDIR_BEGIN
; dir
< DIAGDIR_END
; dir
++) {
217 TileIndex t
= tile
+ TileOffsByDiagDir(dir
);
218 if (!IsValidTile(t
)) continue;
219 if (GetTropicZone(t
) != TROPICZONE_DESERT
) return true;
220 if (HasTileWaterClass(t
) && GetWaterClass(t
) == WATER_CLASS_SEA
) return true;
225 static void TileLoopClearDesert(TileIndex tile
)
227 /* Current desert level - 0 if it is not desert */
229 if (IsClearGround(tile
, CLEAR_DESERT
)) current
= GetClearDensity(tile
);
231 /* Expected desert level - 0 if it shouldn't be desert */
233 if (GetTropicZone(tile
) == TROPICZONE_DESERT
) {
234 expected
= NeighbourIsNormal(tile
) ? 1 : 3;
237 if (current
== expected
) return;
240 SetClearGroundDensity(tile
, CLEAR_GRASS
, 3);
242 /* Transition from clear to desert is not smooth (after clearing desert tile) */
243 SetClearGroundDensity(tile
, CLEAR_DESERT
, expected
);
246 MarkTileDirtyByTile(tile
);
249 static void TileLoop_Clear(TileIndex tile
)
251 /* If the tile is at any edge flood it to prevent maps without water. */
252 if (_settings_game
.construction
.freeform_edges
&& DistanceFromEdge(tile
) == 1) {
254 if (IsTileFlat(tile
, &z
) && z
== 0) {
256 MarkTileDirtyByTile(tile
);
260 AmbientSoundEffect(tile
);
262 switch (_settings_game
.game_creation
.landscape
) {
263 case LT_TROPIC
: TileLoopClearDesert(tile
); break;
264 case LT_ARCTIC
: TileLoopClearAlps(tile
); break;
267 switch (GetClearGround(tile
)) {
269 if (GetClearDensity(tile
) == 3) return;
271 if (_game_mode
!= GM_EDITOR
) {
272 if (GetClearCounter(tile
) < 7) {
273 AddClearCounter(tile
, 1);
276 SetClearCounter(tile
, 0);
277 AddClearDensity(tile
, 1);
280 SetClearGroundDensity(tile
, GB(Random(), 0, 8) > 21 ? CLEAR_GRASS
: CLEAR_ROUGH
, 3);
287 if (_game_mode
== GM_EDITOR
) return;
289 if (GetClearCounter(tile
) < 7) {
290 AddClearCounter(tile
, 1);
293 SetClearCounter(tile
, 0);
296 if (GetIndustryIndexOfField(tile
) == INVALID_INDUSTRY
&& GetFieldType(tile
) >= 7) {
297 /* This farmfield is no longer farmfield, so make it grass again */
298 MakeClear(tile
, CLEAR_GRASS
, 2);
300 uint field_type
= GetFieldType(tile
);
301 field_type
= (field_type
< 8) ? field_type
+ 1 : 0;
302 SetFieldType(tile
, field_type
);
310 MarkTileDirtyByTile(tile
);
313 void GenerateClearTile()
318 /* add rough tiles */
319 i
= ScaleByMapSize(GB(Random(), 0, 10) + 0x400);
320 gi
= ScaleByMapSize(GB(Random(), 0, 7) + 0x80);
322 SetGeneratingWorldProgress(GWP_ROUGH_ROCKY
, gi
+ i
);
324 IncreaseGeneratingWorldProgress(GWP_ROUGH_ROCKY
);
326 if (IsTileType(tile
, MP_CLEAR
) && !IsClearGround(tile
, CLEAR_DESERT
)) SetClearGroundDensity(tile
, CLEAR_ROUGH
, 3);
329 /* add rocky tiles */
333 tile
= RandomTileSeed(r
);
335 IncreaseGeneratingWorldProgress(GWP_ROUGH_ROCKY
);
336 if (IsTileType(tile
, MP_CLEAR
) && !IsClearGround(tile
, CLEAR_DESERT
)) {
337 uint j
= GB(r
, 16, 4) + 5;
341 SetClearGroundDensity(tile
, CLEAR_ROCKS
, 3);
342 MarkTileDirtyByTile(tile
);
344 if (--j
== 0) goto get_out
;
345 tile_new
= tile
+ TileOffsByDiagDir((DiagDirection
)GB(Random(), 0, 2));
346 } while (!IsTileType(tile_new
, MP_CLEAR
) || IsClearGround(tile_new
, CLEAR_DESERT
));
354 static TrackStatus
GetTileTrackStatus_Clear(TileIndex tile
, TransportType mode
, uint sub_mode
, DiagDirection side
)
359 static const StringID _clear_land_str
[] = {
360 STR_LAI_CLEAR_DESCRIPTION_GRASS
,
361 STR_LAI_CLEAR_DESCRIPTION_ROUGH_LAND
,
362 STR_LAI_CLEAR_DESCRIPTION_ROCKS
,
363 STR_LAI_CLEAR_DESCRIPTION_FIELDS
,
364 STR_LAI_CLEAR_DESCRIPTION_SNOW_COVERED_LAND
,
365 STR_LAI_CLEAR_DESCRIPTION_DESERT
368 static void GetTileDesc_Clear(TileIndex tile
, TileDesc
*td
)
370 if (IsClearGround(tile
, CLEAR_GRASS
) && GetClearDensity(tile
) == 0) {
371 td
->str
= STR_LAI_CLEAR_DESCRIPTION_BARE_LAND
;
373 td
->str
= _clear_land_str
[GetClearGround(tile
)];
375 td
->owner
[0] = GetTileOwner(tile
);
378 static void ChangeTileOwner_Clear(TileIndex tile
, Owner old_owner
, Owner new_owner
)
383 static CommandCost
TerraformTile_Clear(TileIndex tile
, DoCommandFlag flags
, int z_new
, Slope tileh_new
)
385 return Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
);
388 extern const TileTypeProcs _tile_type_clear_procs
= {
389 DrawTile_Clear
, ///< draw_tile_proc
390 GetSlopePixelZ_Clear
, ///< get_slope_z_proc
391 ClearTile_Clear
, ///< clear_tile_proc
392 nullptr, ///< add_accepted_cargo_proc
393 GetTileDesc_Clear
, ///< get_tile_desc_proc
394 GetTileTrackStatus_Clear
, ///< get_tile_track_status_proc
395 nullptr, ///< click_tile_proc
396 nullptr, ///< animate_tile_proc
397 TileLoop_Clear
, ///< tile_loop_proc
398 ChangeTileOwner_Clear
, ///< change_tile_owner_proc
399 nullptr, ///< add_produced_cargo_proc
400 nullptr, ///< vehicle_enter_tile_proc
401 GetFoundation_Clear
, ///< get_foundation_proc
402 TerraformTile_Clear
, ///< terraform_tile_proc