4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file tree_cmd.cpp Handling of tree tiles. */
13 #include "map/ground.h"
14 #include "map/slope.h"
15 #include "landscape.h"
16 #include "viewport_func.h"
17 #include "command_func.h"
20 #include "clear_func.h"
21 #include "company_func.h"
23 #include "company_base.h"
24 #include "core/random_func.hpp"
25 #include "newgrf_generic.h"
27 #include "table/strings.h"
28 #include "table/tree_land.h"
31 * List of tree placer algorithm.
33 * This enumeration defines all possible tree placer algorithm in the game.
36 TP_NONE
, ///< No tree placer algorithm
37 TP_ORIGINAL
, ///< The original algorithm
38 TP_IMPROVED
, ///< A 'improved' algorithm
41 /** Where to place trees while in-game? */
42 enum ExtraTreePlacement
{
43 ETP_NONE
, ///< Place trees on no tiles
44 ETP_RAINFOREST
, ///< Place trees only on rainforest tiles
45 ETP_ALL
, ///< Place trees on all tiles
48 /** Determines when to consider building more trees. */
51 static const uint16 DEFAULT_TREE_STEPS
= 1000; ///< Default number of attempts for placing trees.
52 static const uint16 DEFAULT_RAINFOREST_TREE_STEPS
= 15000; ///< Default number of attempts for placing extra trees at rainforest in tropic.
53 static const uint16 EDITOR_TREE_DIV
= 5; ///< Game editor tree generation divisor factor.
56 * Tests if a tile can be converted to have trees
57 * This is true for clear ground without farms or rocks.
59 * @param tile the tile of interest
60 * @param allow_desert Allow planting trees on GROUND_DESERT?
61 * @return true if trees can be built.
63 static bool CanPlantTreesOnTile(TileIndex tile
, bool allow_desert
)
65 switch (GetTileType(tile
)) {
67 return !IsBridgeAbove(tile
) && IsCoast(tile
) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile
));
70 return IsTileSubtype(tile
, TT_GROUND_CLEAR
) && !IsBridgeAbove(tile
) && GetRawClearGround(tile
) != GROUND_ROCKS
&&
71 (allow_desert
|| !IsClearGround(tile
, GROUND_DESERT
));
73 default: return false;
79 * Ground type and density is preserved.
81 * @pre the tile must be suitable for trees.
83 * @param tile where to plant the trees.
84 * @param treetype The type of the tree
85 * @param count the number of trees (minus 1)
86 * @param growth the growth status
88 static void PlantTreesOnTile(TileIndex tile
, TreeType treetype
, uint count
, uint growth
)
90 assert(treetype
!= TREE_INVALID
);
91 assert(CanPlantTreesOnTile(tile
, true));
96 switch (GetTileType(tile
)) {
98 ground
= GROUND_SHORE
;
103 assert(IsTileSubtype(tile
, TT_GROUND_CLEAR
));
104 ground
= GetFullClearGround(tile
);
105 density
= GetClearDensity(tile
);
108 default: NOT_REACHED();
111 MakeTree(tile
, treetype
, count
, growth
, ground
, density
);
114 void AddNeighbouringTree(TileIndex tile
)
116 /* Don't plant extra trees if that's not allowed. */
117 if ((_settings_game
.game_creation
.landscape
== LT_TROPIC
&& GetTropicZone(tile
) == TROPICZONE_RAINFOREST
) ?
118 _settings_game
.construction
.extra_tree_placement
== ETP_NONE
:
119 _settings_game
.construction
.extra_tree_placement
!= ETP_ALL
) {
123 TreeType treetype
= GetTreeType(tile
);
125 tile
+= TileOffsByDir((Direction
)(Random() & 7));
127 /* Cacti don't spread */
128 if (!CanPlantTreesOnTile(tile
, false)) return;
130 /* Don't plant trees, if ground was freshly cleared */
131 if (IsClearTile(tile
) && GetClearGround(tile
) == GROUND_GRASS
&& GetClearDensity(tile
) != 3) return;
133 PlantTreesOnTile(tile
, treetype
, 0, 0);
137 * Get a random TreeType for the given tile based on a given seed
139 * This function returns a random TreeType which can be placed on the given tile.
140 * The seed for randomness must be less or equal 256, use #GB on the value of Random()
141 * to get such a value.
143 * @param tile The tile to get a random TreeType from
144 * @param seed The seed for randomness, must be less or equal 256
145 * @return The random tree type
147 static TreeType
GetRandomTreeType(TileIndex tile
, uint seed
)
149 switch (_settings_game
.game_creation
.landscape
) {
151 return (TreeType
)(seed
* TREE_COUNT_TEMPERATE
/ 256 + TREE_TEMPERATE
);
154 return (TreeType
)(seed
* TREE_COUNT_SUB_ARCTIC
/ 256 + TREE_SUB_ARCTIC
);
157 switch (GetTropicZone(tile
)) {
158 case TROPICZONE_NORMAL
: return (TreeType
)(seed
* TREE_COUNT_SUB_TROPICAL
/ 256 + TREE_SUB_TROPICAL
);
159 case TROPICZONE_DESERT
: return (TreeType
)((seed
> 12) ? TREE_INVALID
: TREE_CACTUS
);
160 default: return (TreeType
)(seed
* TREE_COUNT_RAINFOREST
/ 256 + TREE_RAINFOREST
);
164 return (TreeType
)(seed
* TREE_COUNT_TOYLAND
/ 256 + TREE_TOYLAND
);
169 * Make a random tree tile of the given tile
171 * Create a new tree-tile for the given tile. The second parameter is used for
172 * randomness like type and number of trees.
174 * @param tile The tile to make a tree-tile from
175 * @param r The randomness value from a Random() value
177 static void PlaceTree(TileIndex tile
, uint32 r
)
179 TreeType tree
= GetRandomTreeType(tile
, GB(r
, 24, 8));
181 if (tree
!= TREE_INVALID
) {
182 PlantTreesOnTile(tile
, tree
, GB(r
, 22, 2), min(GB(r
, 16, 3), 6));
184 /* Rerandomize ground, if neither snow nor shore */
185 Ground ground
= GetClearGround(tile
);
186 if (ground
== GROUND_GRASS
|| ground
== GROUND_ROUGH
) {
187 SetClearGroundDensity(tile
, GB(r
, 28, 1) ? GROUND_ROUGH
: GROUND_GRASS
, 3);
190 /* Set the counter to a random start value */
191 SetClearCounter(tile
, (Ground
)GB(r
, 24, 4));
196 * Creates a number of tree groups.
197 * The number of trees in each group depends on how many trees are actually placed around the given tile.
199 * @param num_groups Number of tree groups to place.
201 static void PlaceTreeGroups(uint num_groups
)
204 TileIndex center_tile
= RandomTile();
206 for (uint i
= 0; i
< DEFAULT_TREE_STEPS
; i
++) {
208 int x
= GB(r
, 0, 5) - 16;
209 int y
= GB(r
, 8, 5) - 16;
210 uint dist
= abs(x
) + abs(y
);
211 TileIndex cur_tile
= TileAddWrap(center_tile
, x
, y
);
213 IncreaseGeneratingWorldProgress(GWP_TREE
);
215 if (cur_tile
!= INVALID_TILE
&& dist
<= 13 && CanPlantTreesOnTile(cur_tile
, true)) {
216 PlaceTree(cur_tile
, r
);
220 } while (--num_groups
);
224 * Place a tree at the same height as an existing tree.
226 * Add a new tree around the given tile which is at the same
227 * height or at some offset (2 units) of it.
229 * @param tile The base tile to add a new tree somewhere around
230 * @param height The height (like the one from the tile)
232 static void PlaceTreeAtSameHeight(TileIndex tile
, int height
)
234 for (uint i
= 0; i
< DEFAULT_TREE_STEPS
; i
++) {
236 int x
= GB(r
, 0, 5) - 16;
237 int y
= GB(r
, 8, 5) - 16;
238 TileIndex cur_tile
= TileAddWrap(tile
, x
, y
);
239 if (cur_tile
== INVALID_TILE
) continue;
241 /* Keep in range of the existing tree */
242 if (abs(x
) + abs(y
) > 16) continue;
244 /* Clear tile, no farm-tiles or rocks */
245 if (!CanPlantTreesOnTile(cur_tile
, true)) continue;
247 /* Not too much height difference */
248 if (Delta(GetTileZ(cur_tile
), height
) > 2) continue;
250 /* Place one tree and quit */
251 PlaceTree(cur_tile
, r
);
257 * Place some trees randomly
259 * This function just place some trees randomly on the map.
261 void PlaceTreesRandomly()
265 i
= ScaleByMapSize(DEFAULT_TREE_STEPS
);
266 if (_game_mode
== GM_EDITOR
) i
/= EDITOR_TREE_DIV
;
269 TileIndex tile
= RandomTileSeed(r
);
271 IncreaseGeneratingWorldProgress(GWP_TREE
);
273 if (CanPlantTreesOnTile(tile
, true)) {
275 if (_settings_game
.game_creation
.tree_placer
!= TP_IMPROVED
) continue;
277 /* Place a number of trees based on the tile height.
278 * This gives a cool effect of multiple trees close together.
279 * It is almost real life ;) */
281 /* The higher we get, the more trees we plant */
282 j
= GetTileZ(tile
) * 2;
283 /* Above snowline more trees! */
284 if (_settings_game
.game_creation
.landscape
== LT_ARCTIC
&& ht
> GetSnowLine()) j
*= 3;
286 PlaceTreeAtSameHeight(tile
, ht
);
291 /* place extra trees at rainforest area */
292 if (_settings_game
.game_creation
.landscape
== LT_TROPIC
) {
293 i
= ScaleByMapSize(DEFAULT_RAINFOREST_TREE_STEPS
);
294 if (_game_mode
== GM_EDITOR
) i
/= EDITOR_TREE_DIV
;
298 TileIndex tile
= RandomTileSeed(r
);
300 IncreaseGeneratingWorldProgress(GWP_TREE
);
302 if (GetTropicZone(tile
) == TROPICZONE_RAINFOREST
&& CanPlantTreesOnTile(tile
, false)) {
312 * This function takes care of the selected tree placer algorithm and
313 * place randomly the trees for a new game.
319 if (_settings_game
.game_creation
.tree_placer
== TP_NONE
) return;
321 switch (_settings_game
.game_creation
.tree_placer
) {
322 case TP_ORIGINAL
: i
= _settings_game
.game_creation
.landscape
== LT_ARCTIC
? 15 : 6; break;
323 case TP_IMPROVED
: i
= _settings_game
.game_creation
.landscape
== LT_ARCTIC
? 4 : 2; break;
324 default: NOT_REACHED();
327 total
= ScaleByMapSize(DEFAULT_TREE_STEPS
);
328 if (_settings_game
.game_creation
.landscape
== LT_TROPIC
) total
+= ScaleByMapSize(DEFAULT_RAINFOREST_TREE_STEPS
);
330 uint num_groups
= (_settings_game
.game_creation
.landscape
!= LT_TOYLAND
) ? ScaleByMapSize(GB(Random(), 0, 5) + 25) : 0;
331 total
+= num_groups
* DEFAULT_TREE_STEPS
;
332 SetGeneratingWorldProgress(GWP_TREE
, total
);
334 if (num_groups
!= 0) PlaceTreeGroups(num_groups
);
336 for (; i
!= 0; i
--) {
337 PlaceTreesRandomly();
343 * @param tile start tile of area-drag of tree plantation
344 * @param flags type of operation
345 * @param p1 tree type, TREE_INVALID means random.
346 * @param p2 end tile of area-drag
348 * @return the cost of this operation or an error
350 CommandCost
CmdPlantTree(TileIndex tile
, DoCommandFlag flags
, uint32 p1
, uint32 p2
, const char *text
)
352 StringID msg
= INVALID_STRING_ID
;
353 CommandCost
cost(EXPENSES_OTHER
);
354 const byte tree_to_plant
= GB(p1
, 0, 8); // We cannot use Extract as min and max are climate specific.
356 if (p2
>= MapSize()) return CMD_ERROR
;
357 /* Check the tree type within the current climate */
358 if (tree_to_plant
!= TREE_INVALID
&& !IsInsideBS(tree_to_plant
, _tree_base_by_landscape
[_settings_game
.game_creation
.landscape
], _tree_count_by_landscape
[_settings_game
.game_creation
.landscape
])) return CMD_ERROR
;
360 Company
*c
= (_game_mode
!= GM_EDITOR
) ? Company::GetIfValid(_current_company
) : NULL
;
361 int limit
= (c
== NULL
? INT32_MAX
: GB(c
->tree_limit
, 16, 16));
363 TileArea
ta(tile
, p2
);
364 TILE_AREA_LOOP(tile
, ta
) {
365 switch (GetTileType(tile
)) {
367 if (!IsTreeTile(tile
)) break;
369 /* no more space for trees? */
370 if (_game_mode
!= GM_EDITOR
&& GetTreeCount(tile
) == 4) {
371 msg
= STR_ERROR_TREE_ALREADY_HERE
;
375 /* Test tree limit. */
377 msg
= STR_ERROR_TREE_PLANT_LIMIT_REACHED
;
381 if (flags
& DC_EXEC
) {
382 AddTreeCount(tile
, 1);
383 MarkTileDirtyByTile(tile
);
384 if (c
!= NULL
) c
->tree_limit
-= 1 << 16;
386 /* 2x as expensive to add more trees to an existing tile */
387 cost
.AddCost(_price
[PR_BUILD_TREES
] * 2);
391 if (!IsCoast(tile
) || IsSlopeWithOneCornerRaised(GetTileSlope(tile
))) {
392 msg
= STR_ERROR_CAN_T_BUILD_ON_WATER
;
398 msg
= STR_ERROR_SITE_UNSUITABLE
;
402 if (IsBridgeAbove(tile
)) {
403 msg
= STR_ERROR_SITE_UNSUITABLE
;
407 TreeType treetype
= (TreeType
)tree_to_plant
;
408 /* Be a bit picky about which trees go where. */
409 if (_settings_game
.game_creation
.landscape
== LT_TROPIC
&& treetype
!= TREE_INVALID
&& (
410 /* No cacti outside the desert */
411 (treetype
== TREE_CACTUS
&& GetTropicZone(tile
) != TROPICZONE_DESERT
) ||
412 /* No rain forest trees outside the rain forest, except in the editor mode where it makes those tiles rain forest tile */
413 (IsInsideMM(treetype
, TREE_RAINFOREST
, TREE_CACTUS
) && GetTropicZone(tile
) != TROPICZONE_RAINFOREST
&& _game_mode
!= GM_EDITOR
) ||
414 /* And no subtropical trees in the desert/rain forest */
415 (IsInsideMM(treetype
, TREE_SUB_TROPICAL
, TREE_TOYLAND
) && GetTropicZone(tile
) != TROPICZONE_NORMAL
))) {
416 msg
= STR_ERROR_TREE_WRONG_TERRAIN_FOR_TREE_TYPE
;
420 /* Test tree limit. */
422 msg
= STR_ERROR_TREE_PLANT_LIMIT_REACHED
;
426 if (IsTileType(tile
, TT_GROUND
)) {
427 /* Remove fields or rocks. Note that the ground will get barrened */
428 if (IsTileSubtype(tile
, TT_GROUND_FIELDS
) || GetRawClearGround(tile
) == GROUND_ROCKS
) {
429 CommandCost ret
= DoCommand(tile
, 0, 0, flags
, CMD_LANDSCAPE_CLEAR
);
430 if (ret
.Failed()) return ret
;
435 if (_game_mode
!= GM_EDITOR
&& Company::IsValidID(_current_company
)) {
436 Town
*t
= ClosestTownFromTile(tile
, _settings_game
.economy
.dist_local_authority
);
437 if (t
!= NULL
) ChangeTownRating(t
, RATING_TREE_UP_STEP
, RATING_TREE_MAXIMUM
, flags
);
440 if (flags
& DC_EXEC
) {
441 if (treetype
== TREE_INVALID
) {
442 treetype
= GetRandomTreeType(tile
, GB(Random(), 24, 8));
443 if (treetype
== TREE_INVALID
) treetype
= TREE_CACTUS
;
446 /* Plant full grown trees in scenario editor */
447 PlantTreesOnTile(tile
, treetype
, 0, _game_mode
== GM_EDITOR
? 3 : 0);
448 MarkTileDirtyByTile(tile
);
449 if (c
!= NULL
) c
->tree_limit
-= 1 << 16;
451 /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
452 if (_game_mode
== GM_EDITOR
&& IsInsideMM(treetype
, TREE_RAINFOREST
, TREE_CACTUS
)) {
453 SetTropicZone(tile
, TROPICZONE_RAINFOREST
);
457 cost
.AddCost(_price
[PR_BUILD_TREES
]);
459 /* Tree limit used up? No need to check more. */
460 if (limit
< 0) break;
463 if (cost
.GetCost() == 0) {
464 return_cmd_error(msg
);
472 /* Don't place trees if that's not allowed */
473 if (_settings_game
.construction
.extra_tree_placement
== ETP_NONE
) return;
479 /* place a tree at a random rainforest spot */
480 if (_settings_game
.game_creation
.landscape
== LT_TROPIC
&&
481 (r
= Random(), tile
= RandomTileSeed(r
), GetTropicZone(tile
) == TROPICZONE_RAINFOREST
) &&
482 CanPlantTreesOnTile(tile
, false) &&
483 (tree
= GetRandomTreeType(tile
, GB(r
, 24, 8))) != TREE_INVALID
) {
484 PlantTreesOnTile(tile
, tree
, 0, 0);
488 if (--_trees_tick_ctr
!= 0 || _settings_game
.construction
.extra_tree_placement
!= ETP_ALL
) return;
490 /* place a tree at a random spot */
492 tile
= RandomTileSeed(r
);
493 if (CanPlantTreesOnTile(tile
, false) && (tree
= GetRandomTreeType(tile
, GB(r
, 24, 8))) != TREE_INVALID
) {
494 PlantTreesOnTile(tile
, tree
, 0, 0);
498 void InitializeTrees()