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"
16 #include "core/random_func.hpp"
17 #include "newgrf_generic.h"
18 #include "landscape_cmd.h"
20 #include "table/strings.h"
21 #include "table/sprites.h"
22 #include "table/clear_land.h"
24 #include "safeguards.h"
26 static CommandCost
ClearTile_Clear(TileIndex tile
, DoCommandFlag flags
)
28 static const Price clear_price_table
[] = {
36 CommandCost
price(EXPENSES_CONSTRUCTION
);
38 if (!IsClearGround(tile
, CLEAR_GRASS
) || GetClearDensity(tile
) != 0) {
39 price
.AddCost(_price
[clear_price_table
[GetClearGround(tile
)]]);
42 if (flags
& DC_EXEC
) DoClearSquare(tile
);
47 void DrawClearLandTile(const TileInfo
*ti
, byte set
)
49 DrawGroundSprite(SPR_FLAT_BARE_LAND
+ SlopeToSpriteOffset(ti
->tileh
) + set
* 19, PAL_NONE
);
52 void DrawHillyLandTile(const TileInfo
*ti
)
54 if (ti
->tileh
!= SLOPE_FLAT
) {
55 DrawGroundSprite(SPR_FLAT_ROUGH_LAND
+ SlopeToSpriteOffset(ti
->tileh
), PAL_NONE
);
57 DrawGroundSprite(_landscape_clear_sprites_rough
[GB(TileHash(ti
->x
, ti
->y
), 0, 3)], PAL_NONE
);
61 static void DrawClearLandFence(const TileInfo
*ti
)
63 /* combine fences into one sprite object */
66 int maxz
= GetSlopeMaxPixelZ(ti
->tileh
);
68 uint fence_nw
= GetFence(ti
->tile
, DIAGDIR_NW
);
70 int z
= GetSlopePixelZInCorner(ti
->tileh
, CORNER_W
);
71 SpriteID sprite
= _clear_land_fence_sprites
[fence_nw
- 1] + _fence_mod_by_tileh_nw
[ti
->tileh
];
72 AddSortableSpriteToDraw(sprite
, PAL_NONE
, ti
->x
, ti
->y
- 16, 16, 32, maxz
- z
+ 4, ti
->z
+ z
, false, 0, 16, -z
);
75 uint fence_ne
= GetFence(ti
->tile
, DIAGDIR_NE
);
77 int z
= GetSlopePixelZInCorner(ti
->tileh
, CORNER_E
);
78 SpriteID sprite
= _clear_land_fence_sprites
[fence_ne
- 1] + _fence_mod_by_tileh_ne
[ti
->tileh
];
79 AddSortableSpriteToDraw(sprite
, PAL_NONE
, ti
->x
- 16, ti
->y
, 32, 16, maxz
- z
+ 4, ti
->z
+ z
, false, 16, 0, -z
);
82 uint fence_sw
= GetFence(ti
->tile
, DIAGDIR_SW
);
83 uint fence_se
= GetFence(ti
->tile
, DIAGDIR_SE
);
85 if (fence_sw
!= 0 || fence_se
!= 0) {
86 int z
= GetSlopePixelZInCorner(ti
->tileh
, CORNER_S
);
89 SpriteID sprite
= _clear_land_fence_sprites
[fence_sw
- 1] + _fence_mod_by_tileh_sw
[ti
->tileh
];
90 AddSortableSpriteToDraw(sprite
, PAL_NONE
, ti
->x
, ti
->y
, 16, 16, maxz
- z
+ 4, ti
->z
+ z
, false, 0, 0, -z
);
94 SpriteID sprite
= _clear_land_fence_sprites
[fence_se
- 1] + _fence_mod_by_tileh_se
[ti
->tileh
];
95 AddSortableSpriteToDraw(sprite
, PAL_NONE
, ti
->x
, ti
->y
, 16, 16, maxz
- z
+ 4, ti
->z
+ z
, false, 0, 0, -z
);
101 static void DrawTile_Clear(TileInfo
*ti
)
103 switch (GetClearGround(ti
->tile
)) {
105 DrawClearLandTile(ti
, GetClearDensity(ti
->tile
));
109 DrawHillyLandTile(ti
);
113 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
);
117 DrawGroundSprite(_clear_land_sprites_farmland
[GetFieldType(ti
->tile
)] + SlopeToSpriteOffset(ti
->tileh
), PAL_NONE
);
118 DrawClearLandFence(ti
);
123 DrawGroundSprite(_clear_land_sprites_snow_desert
[GetClearDensity(ti
->tile
)] + SlopeToSpriteOffset(ti
->tileh
), PAL_NONE
);
127 DrawBridgeMiddle(ti
);
130 static int GetSlopePixelZ_Clear(TileIndex tile
, uint x
, uint y
, bool)
133 Slope tileh
= GetTilePixelSlope(tile
, &z
);
135 return z
+ GetPartialPixelZ(x
& 0xF, y
& 0xF, tileh
);
138 static Foundation
GetFoundation_Clear(TileIndex
, Slope
)
140 return FOUNDATION_NONE
;
143 static void UpdateFences(TileIndex tile
)
145 assert(IsTileType(tile
, MP_CLEAR
) && IsClearGround(tile
, CLEAR_FIELDS
));
148 bool neighbour
= (IsTileType(TILE_ADDXY(tile
, 1, 0), MP_CLEAR
) && IsClearGround(TILE_ADDXY(tile
, 1, 0), CLEAR_FIELDS
));
149 if (!neighbour
&& GetFence(tile
, DIAGDIR_SW
) == 0) {
150 SetFence(tile
, DIAGDIR_SW
, 3);
154 neighbour
= (IsTileType(TILE_ADDXY(tile
, 0, 1), MP_CLEAR
) && IsClearGround(TILE_ADDXY(tile
, 0, 1), CLEAR_FIELDS
));
155 if (!neighbour
&& GetFence(tile
, DIAGDIR_SE
) == 0) {
156 SetFence(tile
, DIAGDIR_SE
, 3);
160 neighbour
= (IsTileType(TILE_ADDXY(tile
, -1, 0), MP_CLEAR
) && IsClearGround(TILE_ADDXY(tile
, -1, 0), CLEAR_FIELDS
));
161 if (!neighbour
&& GetFence(tile
, DIAGDIR_NE
) == 0) {
162 SetFence(tile
, DIAGDIR_NE
, 3);
166 neighbour
= (IsTileType(TILE_ADDXY(tile
, 0, -1), MP_CLEAR
) && IsClearGround(TILE_ADDXY(tile
, 0, -1), CLEAR_FIELDS
));
167 if (!neighbour
&& GetFence(tile
, DIAGDIR_NW
) == 0) {
168 SetFence(tile
, DIAGDIR_NW
, 3);
172 if (dirty
) MarkTileDirtyByTile(tile
);
176 /** Convert to or from snowy tiles. */
177 static void TileLoopClearAlps(TileIndex tile
)
179 int k
= GetTileZ(tile
) - GetSnowLine() + 1;
182 /* Below the snow line, do nothing if no snow. */
183 if (!IsSnowTile(tile
)) return;
185 /* At or above the snow line, make snow tile if needed. */
186 if (!IsSnowTile(tile
)) {
188 MarkTileDirtyByTile(tile
);
192 /* Update snow density. */
193 uint current_density
= GetClearDensity(tile
);
194 uint req_density
= (k
< 0) ? 0u : std::min
<uint
>(k
, 3u);
196 if (current_density
< req_density
) {
197 AddClearDensity(tile
, 1);
198 } else if (current_density
> req_density
) {
199 AddClearDensity(tile
, -1);
201 /* Density at the required level. */
205 MarkTileDirtyByTile(tile
);
209 * Tests if at least one surrounding tile is non-desert
210 * @param tile tile to check
211 * @return does this tile have at least one non-desert tile around?
213 static inline bool NeighbourIsNormal(TileIndex tile
)
215 for (DiagDirection dir
= DIAGDIR_BEGIN
; dir
< DIAGDIR_END
; dir
++) {
216 TileIndex t
= tile
+ TileOffsByDiagDir(dir
);
217 if (!IsValidTile(t
)) continue;
218 if (GetTropicZone(t
) != TROPICZONE_DESERT
) return true;
219 if (HasTileWaterClass(t
) && GetWaterClass(t
) == WATER_CLASS_SEA
) return true;
224 static void TileLoopClearDesert(TileIndex tile
)
226 /* Current desert level - 0 if it is not desert */
228 if (IsClearGround(tile
, CLEAR_DESERT
)) current
= GetClearDensity(tile
);
230 /* Expected desert level - 0 if it shouldn't be desert */
232 if (GetTropicZone(tile
) == TROPICZONE_DESERT
) {
233 expected
= NeighbourIsNormal(tile
) ? 1 : 3;
236 if (current
== expected
) return;
239 SetClearGroundDensity(tile
, CLEAR_GRASS
, 3);
241 /* Transition from clear to desert is not smooth (after clearing desert tile) */
242 SetClearGroundDensity(tile
, CLEAR_DESERT
, expected
);
245 MarkTileDirtyByTile(tile
);
248 static void TileLoop_Clear(TileIndex tile
)
250 AmbientSoundEffect(tile
);
252 switch (_settings_game
.game_creation
.landscape
) {
253 case LT_TROPIC
: TileLoopClearDesert(tile
); break;
254 case LT_ARCTIC
: TileLoopClearAlps(tile
); break;
257 switch (GetClearGround(tile
)) {
259 if (GetClearDensity(tile
) == 3) return;
261 if (_game_mode
!= GM_EDITOR
) {
262 if (GetClearCounter(tile
) < 7) {
263 AddClearCounter(tile
, 1);
266 SetClearCounter(tile
, 0);
267 AddClearDensity(tile
, 1);
270 SetClearGroundDensity(tile
, GB(Random(), 0, 8) > 21 ? CLEAR_GRASS
: CLEAR_ROUGH
, 3);
277 if (_game_mode
== GM_EDITOR
) return;
279 if (GetClearCounter(tile
) < 7) {
280 AddClearCounter(tile
, 1);
283 SetClearCounter(tile
, 0);
286 if (GetIndustryIndexOfField(tile
) == INVALID_INDUSTRY
&& GetFieldType(tile
) >= 7) {
287 /* This farmfield is no longer farmfield, so make it grass again */
288 MakeClear(tile
, CLEAR_GRASS
, 2);
290 uint field_type
= GetFieldType(tile
);
291 field_type
= (field_type
< 8) ? field_type
+ 1 : 0;
292 SetFieldType(tile
, field_type
);
300 MarkTileDirtyByTile(tile
);
303 void GenerateClearTile()
308 /* add rough tiles */
309 i
= Map::ScaleBySize(GB(Random(), 0, 10) + 0x400);
310 gi
= Map::ScaleBySize(GB(Random(), 0, 7) + 0x80);
312 SetGeneratingWorldProgress(GWP_ROUGH_ROCKY
, gi
+ i
);
314 IncreaseGeneratingWorldProgress(GWP_ROUGH_ROCKY
);
316 if (IsTileType(tile
, MP_CLEAR
) && !IsClearGround(tile
, CLEAR_DESERT
)) SetClearGroundDensity(tile
, CLEAR_ROUGH
, 3);
319 /* add rocky tiles */
322 uint32_t r
= Random();
323 tile
= RandomTileSeed(r
);
325 IncreaseGeneratingWorldProgress(GWP_ROUGH_ROCKY
);
326 if (IsTileType(tile
, MP_CLEAR
) && !IsClearGround(tile
, CLEAR_DESERT
)) {
327 uint j
= GB(r
, 16, 4) + 5;
331 SetClearGroundDensity(tile
, CLEAR_ROCKS
, 3);
332 MarkTileDirtyByTile(tile
);
334 if (--j
== 0) goto get_out
;
335 tile_new
= tile
+ TileOffsByDiagDir((DiagDirection
)GB(Random(), 0, 2));
336 } while (!IsTileType(tile_new
, MP_CLEAR
) || IsClearGround(tile_new
, CLEAR_DESERT
));
344 static TrackStatus
GetTileTrackStatus_Clear(TileIndex
, TransportType
, uint
, DiagDirection
)
349 static const StringID _clear_land_str
[] = {
350 STR_LAI_CLEAR_DESCRIPTION_GRASS
,
351 STR_LAI_CLEAR_DESCRIPTION_ROUGH_LAND
,
352 STR_LAI_CLEAR_DESCRIPTION_ROCKS
,
353 STR_LAI_CLEAR_DESCRIPTION_FIELDS
,
354 STR_LAI_CLEAR_DESCRIPTION_SNOW_COVERED_LAND
,
355 STR_LAI_CLEAR_DESCRIPTION_DESERT
358 static void GetTileDesc_Clear(TileIndex tile
, TileDesc
*td
)
360 if (IsClearGround(tile
, CLEAR_GRASS
) && GetClearDensity(tile
) == 0) {
361 td
->str
= STR_LAI_CLEAR_DESCRIPTION_BARE_LAND
;
363 td
->str
= _clear_land_str
[GetClearGround(tile
)];
365 td
->owner
[0] = GetTileOwner(tile
);
368 static void ChangeTileOwner_Clear(TileIndex
, Owner
, Owner
)
373 static CommandCost
TerraformTile_Clear(TileIndex tile
, DoCommandFlag flags
, int, Slope
)
375 return Command
<CMD_LANDSCAPE_CLEAR
>::Do(flags
, tile
);
378 extern const TileTypeProcs _tile_type_clear_procs
= {
379 DrawTile_Clear
, ///< draw_tile_proc
380 GetSlopePixelZ_Clear
, ///< get_slope_z_proc
381 ClearTile_Clear
, ///< clear_tile_proc
382 nullptr, ///< add_accepted_cargo_proc
383 GetTileDesc_Clear
, ///< get_tile_desc_proc
384 GetTileTrackStatus_Clear
, ///< get_tile_track_status_proc
385 nullptr, ///< click_tile_proc
386 nullptr, ///< animate_tile_proc
387 TileLoop_Clear
, ///< tile_loop_proc
388 ChangeTileOwner_Clear
, ///< change_tile_owner_proc
389 nullptr, ///< add_produced_cargo_proc
390 nullptr, ///< vehicle_enter_tile_proc
391 GetFoundation_Clear
, ///< get_foundation_proc
392 TerraformTile_Clear
, ///< terraform_tile_proc