(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / tree_cmd.cpp
blobea1656a00af4bfcab041493dae88ccea4e4f56c1
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file tree_cmd.cpp Handling of tree tiles. */
12 #include "stdafx.h"
13 #include "clear_map.h"
14 #include "landscape.h"
15 #include "tree_map.h"
16 #include "viewport_func.h"
17 #include "command_func.h"
18 #include "economy_func.h"
19 #include "town.h"
20 #include "genworld.h"
21 #include "transparency.h"
22 #include "clear_func.h"
23 #include "company_func.h"
24 #include "sound_func.h"
25 #include "water_map.h"
26 #include "water.h"
27 #include "landscape_type.h"
28 #include "company_base.h"
29 #include "core/random_func.hpp"
31 #include "table/strings.h"
32 #include "table/sprites.h"
33 #include "table/tree_land.h"
34 #include "table/clear_land.h"
36 /**
37 * List of tree placer algorithm.
39 * This enumeration defines all possible tree placer algorithm in the game.
41 enum TreePlacer {
42 TP_NONE, ///< No tree placer algorithm
43 TP_ORIGINAL, ///< The original algorithm
44 TP_IMPROVED, ///< A 'improved' algorithm
47 /** Where to place trees while in-game? */
48 enum ExtraTreePlacement {
49 ETP_NONE, ///< Place trees on no tiles
50 ETP_RAINFOREST, ///< Place trees only on rainforest tiles
51 ETP_ALL, ///< Place trees on all tiles
54 /** Determines when to consider building more trees. */
55 byte _trees_tick_ctr;
57 static const uint16 DEFAULT_TREE_STEPS = 1000; ///< Default number of attempts for placing trees.
58 static const uint16 DEFAULT_RAINFOREST_TREE_STEPS = 15000; ///< Default number of attempts for placing extra trees at rainforest in tropic.
59 static const uint16 EDITOR_TREE_DIV = 5; ///< Game editor tree generation divisor factor.
61 /**
62 * Tests if a tile can be converted to MP_TREES
63 * This is true for clear ground without farms or rocks.
65 * @param tile the tile of interest
66 * @param allow_desert Allow planting trees on CLEAR_DESERT?
67 * @return true if trees can be built.
69 static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
71 switch (GetTileType(tile)) {
72 case MP_WATER:
73 return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL));
75 case MP_CLEAR:
76 return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && GetRawClearGround(tile) != CLEAR_ROCKS &&
77 (allow_desert || !IsClearGround(tile, CLEAR_DESERT));
79 default: return false;
83 /**
84 * Creates a tree tile
85 * Ground type and density is preserved.
87 * @pre the tile must be suitable for trees.
89 * @param tile where to plant the trees.
90 * @param treetype The type of the tree
91 * @param count the number of trees (minus 1)
92 * @param growth the growth status
94 static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, uint growth)
96 assert(treetype != TREE_INVALID);
97 assert(CanPlantTreesOnTile(tile, true));
99 TreeGround ground;
100 uint density = 3;
102 switch (GetTileType(tile)) {
103 case MP_WATER:
104 ground = TREE_GROUND_SHORE;
105 break;
107 case MP_CLEAR:
108 switch (GetClearGround(tile)) {
109 case CLEAR_GRASS: ground = TREE_GROUND_GRASS; break;
110 case CLEAR_ROUGH: ground = TREE_GROUND_ROUGH; break;
111 case CLEAR_SNOW: ground = GetRawClearGround(tile) == CLEAR_ROUGH ? TREE_GROUND_ROUGH_SNOW : TREE_GROUND_SNOW_DESERT; break;
112 default: ground = TREE_GROUND_SNOW_DESERT; break;
114 if (GetClearGround(tile) != CLEAR_ROUGH) density = GetClearDensity(tile);
115 break;
117 default: NOT_REACHED();
120 MakeTree(tile, treetype, count, growth, ground, density);
124 * Get a random TreeType for the given tile based on a given seed
126 * This function returns a random TreeType which can be placed on the given tile.
127 * The seed for randomness must be less or equal 256, use #GB on the value of Random()
128 * to get such a value.
130 * @param tile The tile to get a random TreeType from
131 * @param seed The seed for randomness, must be less or equal 256
132 * @return The random tree type
134 static TreeType GetRandomTreeType(TileIndex tile, uint seed)
136 switch (_settings_game.game_creation.landscape) {
137 case LT_TEMPERATE:
138 return (TreeType)(seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE);
140 case LT_ARCTIC:
141 return (TreeType)(seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC);
143 case LT_TROPIC:
144 switch (GetTropicZone(tile)) {
145 case TROPICZONE_NORMAL: return (TreeType)(seed * TREE_COUNT_SUB_TROPICAL / 256 + TREE_SUB_TROPICAL);
146 case TROPICZONE_DESERT: return (TreeType)((seed > 12) ? TREE_INVALID : TREE_CACTUS);
147 default: return (TreeType)(seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST);
150 default:
151 return (TreeType)(seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND);
156 * Make a random tree tile of the given tile
158 * Create a new tree-tile for the given tile. The second parameter is used for
159 * randomness like type and number of trees.
161 * @param tile The tile to make a tree-tile from
162 * @param r The randomness value from a Random() value
164 static void PlaceTree(TileIndex tile, uint32 r)
166 TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
168 if (tree != TREE_INVALID) {
169 PlantTreesOnTile(tile, tree, GB(r, 22, 2), min(GB(r, 16, 3), 6));
171 /* Rerandomize ground, if neither snow nor shore */
172 TreeGround ground = GetTreeGround(tile);
173 if (ground != TREE_GROUND_SNOW_DESERT && ground != TREE_GROUND_ROUGH_SNOW && ground != TREE_GROUND_SHORE) {
174 SetTreeGroundDensity(tile, (TreeGround)GB(r, 28, 1), 3);
177 /* Set the counter to a random start value */
178 SetTreeCounter(tile, (TreeGround)GB(r, 24, 4));
183 * Creates a number of tree groups.
184 * The number of trees in each group depends on how many trees are actually placed around the given tile.
186 * @param num_groups Number of tree groups to place.
188 static void PlaceTreeGroups(uint num_groups)
190 do {
191 TileIndex center_tile = RandomTile();
193 for (uint i = 0; i < DEFAULT_TREE_STEPS; i++) {
194 uint32 r = Random();
195 int x = GB(r, 0, 5) - 16;
196 int y = GB(r, 8, 5) - 16;
197 uint dist = abs(x) + abs(y);
198 TileIndex cur_tile = TileAddWrap(center_tile, x, y);
200 IncreaseGeneratingWorldProgress(GWP_TREE);
202 if (cur_tile != INVALID_TILE && dist <= 13 && CanPlantTreesOnTile(cur_tile, true)) {
203 PlaceTree(cur_tile, r);
207 } while (--num_groups);
211 * Place a tree at the same height as an existing tree.
213 * Add a new tree around the given tile which is at the same
214 * height or at some offset (2 units) of it.
216 * @param tile The base tile to add a new tree somewhere around
217 * @param height The height (like the one from the tile)
219 static void PlaceTreeAtSameHeight(TileIndex tile, uint height)
221 for (uint i = 0; i < DEFAULT_TREE_STEPS; i++) {
222 uint32 r = Random();
223 int x = GB(r, 0, 5) - 16;
224 int y = GB(r, 8, 5) - 16;
225 TileIndex cur_tile = TileAddWrap(tile, x, y);
226 if (cur_tile == INVALID_TILE) continue;
228 /* Keep in range of the existing tree */
229 if (abs(x) + abs(y) > 16) continue;
231 /* Clear tile, no farm-tiles or rocks */
232 if (!CanPlantTreesOnTile(cur_tile, true)) continue;
234 /* Not too much height difference */
235 if (Delta(GetTileZ(cur_tile), height) > 2) continue;
237 /* Place one tree and quit */
238 PlaceTree(cur_tile, r);
239 break;
244 * Place some trees randomly
246 * This function just place some trees randomly on the map.
248 void PlaceTreesRandomly()
250 uint i, j, ht;
252 i = ScaleByMapSize(DEFAULT_TREE_STEPS);
253 if (_game_mode == GM_EDITOR) i /= EDITOR_TREE_DIV;
254 do {
255 uint32 r = Random();
256 TileIndex tile = RandomTileSeed(r);
258 IncreaseGeneratingWorldProgress(GWP_TREE);
260 if (CanPlantTreesOnTile(tile, true)) {
261 PlaceTree(tile, r);
262 if (_settings_game.game_creation.tree_placer != TP_IMPROVED) continue;
264 /* Place a number of trees based on the tile height.
265 * This gives a cool effect of multiple trees close together.
266 * It is almost real life ;) */
267 ht = GetTileZ(tile);
268 /* The higher we get, the more trees we plant */
269 j = GetTileZ(tile) / TILE_HEIGHT * 2;
270 /* Above snowline more trees! */
271 if (_settings_game.game_creation.landscape == LT_ARCTIC && ht > GetSnowLine()) j *= 3;
272 while (j--) {
273 PlaceTreeAtSameHeight(tile, ht);
276 } while (--i);
278 /* place extra trees at rainforest area */
279 if (_settings_game.game_creation.landscape == LT_TROPIC) {
280 i = ScaleByMapSize(DEFAULT_RAINFOREST_TREE_STEPS);
281 if (_game_mode == GM_EDITOR) i /= EDITOR_TREE_DIV;
283 do {
284 uint32 r = Random();
285 TileIndex tile = RandomTileSeed(r);
287 IncreaseGeneratingWorldProgress(GWP_TREE);
289 if (GetTropicZone(tile) == TROPICZONE_RAINFOREST && CanPlantTreesOnTile(tile, false)) {
290 PlaceTree(tile, r);
292 } while (--i);
297 * Place new trees.
299 * This function takes care of the selected tree placer algorithm and
300 * place randomly the trees for a new game.
302 void GenerateTrees()
304 uint i, total;
306 if (_settings_game.game_creation.tree_placer == TP_NONE) return;
308 switch (_settings_game.game_creation.tree_placer) {
309 case TP_ORIGINAL: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 15 : 6; break;
310 case TP_IMPROVED: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 4 : 2; break;
311 default: NOT_REACHED();
314 total = ScaleByMapSize(DEFAULT_TREE_STEPS);
315 if (_settings_game.game_creation.landscape == LT_TROPIC) total += ScaleByMapSize(DEFAULT_RAINFOREST_TREE_STEPS);
316 total *= i;
317 uint num_groups = (_settings_game.game_creation.landscape != LT_TOYLAND) ? ScaleByMapSize(GB(Random(), 0, 5) + 25) : 0;
318 total += num_groups * DEFAULT_TREE_STEPS;
319 SetGeneratingWorldProgress(GWP_TREE, total);
321 if (num_groups != 0) PlaceTreeGroups(num_groups);
323 for (; i != 0; i--) {
324 PlaceTreesRandomly();
329 * Plant a tree.
330 * @param tile start tile of area-drag of tree plantation
331 * @param flags type of operation
332 * @param p1 tree type, TREE_INVALID means random.
333 * @param p2 end tile of area-drag
334 * @param text unused
335 * @return the cost of this operation or an error
337 CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
339 StringID msg = INVALID_STRING_ID;
340 CommandCost cost(EXPENSES_OTHER);
341 const byte tree_to_plant = GB(p1, 0, 8); // We cannot use Extract as min and max are climate specific.
343 if (p2 >= MapSize()) return CMD_ERROR;
344 /* Check the tree type within the current climate */
345 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;
347 TileArea ta(tile, p2);
348 TILE_AREA_LOOP(tile, ta) {
349 switch (GetTileType(tile)) {
350 case MP_TREES:
351 /* no more space for trees? */
352 if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 4) {
353 msg = STR_ERROR_TREE_ALREADY_HERE;
354 continue;
357 if (flags & DC_EXEC) {
358 AddTreeCount(tile, 1);
359 MarkTileDirtyByTile(tile);
361 /* 2x as expensive to add more trees to an existing tile */
362 cost.AddCost(_price[PR_BUILD_TREES] * 2);
363 break;
365 case MP_WATER:
366 if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL))) {
367 msg = STR_ERROR_CAN_T_BUILD_ON_WATER;
368 continue;
370 /* FALL THROUGH */
371 case MP_CLEAR: {
372 if (IsBridgeAbove(tile)) {
373 msg = STR_ERROR_SITE_UNSUITABLE;
374 continue;
377 TreeType treetype = (TreeType)tree_to_plant;
378 /* Be a bit picky about which trees go where. */
379 if (_settings_game.game_creation.landscape == LT_TROPIC && treetype != TREE_INVALID && (
380 /* No cacti outside the desert */
381 (treetype == TREE_CACTUS && GetTropicZone(tile) != TROPICZONE_DESERT) ||
382 /* No rain forest trees outside the rain forest, except in the editor mode where it makes those tiles rain forest tile */
383 (IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS) && GetTropicZone(tile) != TROPICZONE_RAINFOREST && _game_mode != GM_EDITOR) ||
384 /* And no subtropical trees in the desert/rain forest */
385 (IsInsideMM(treetype, TREE_SUB_TROPICAL, TREE_TOYLAND) && GetTropicZone(tile) != TROPICZONE_NORMAL))) {
386 msg = STR_ERROR_TREE_WRONG_TERRAIN_FOR_TREE_TYPE;
387 continue;
390 if (IsTileType(tile, MP_CLEAR)) {
391 /* Remove fields or rocks. Note that the ground will get barrened */
392 switch (GetRawClearGround(tile)) {
393 case CLEAR_FIELDS:
394 case CLEAR_ROCKS: {
395 CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
396 if (ret.Failed()) return ret;
397 cost.AddCost(ret);
398 break;
401 default: break;
405 if (_game_mode != GM_EDITOR && Company::IsValidID(_current_company)) {
406 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
407 if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
410 if (flags & DC_EXEC) {
411 if (treetype == TREE_INVALID) {
412 treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
413 if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
416 /* Plant full grown trees in scenario editor */
417 PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
418 MarkTileDirtyByTile(tile);
420 /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
421 if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS)) {
422 SetTropicZone(tile, TROPICZONE_RAINFOREST);
425 cost.AddCost(_price[PR_BUILD_TREES]);
426 break;
429 default:
430 msg = STR_ERROR_SITE_UNSUITABLE;
431 break;
435 if (cost.GetCost() == 0) {
436 return_cmd_error(msg);
437 } else {
438 return cost;
442 struct TreeListEnt : PalSpriteID {
443 byte x, y;
446 static void DrawTile_Trees(TileInfo *ti)
448 switch (GetTreeGround(ti->tile)) {
449 case TREE_GROUND_SHORE: DrawShoreTile(ti->tileh); break;
450 case TREE_GROUND_GRASS: DrawClearLandTile(ti, GetTreeDensity(ti->tile)); break;
451 case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
452 default: DrawGroundSprite(_clear_land_sprites_snow_desert[GetTreeDensity(ti->tile)] + SlopeToSpriteOffset(ti->tileh), PAL_NONE); break;
455 DrawClearLandFence(ti);
457 /* Do not draw trees when the invisible trees setting is set */
458 if (IsInvisibilitySet(TO_TREES)) return;
460 uint tmp = CountBits(ti->tile + ti->x + ti->y);
461 uint index = GB(tmp, 0, 2) + (GetTreeType(ti->tile) << 2);
463 /* different tree styles above one of the grounds */
464 if ((GetTreeGround(ti->tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(ti->tile) == TREE_GROUND_ROUGH_SNOW) &&
465 GetTreeDensity(ti->tile) >= 2 &&
466 IsInsideMM(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
467 index += 164 - (TREE_SUB_ARCTIC << 2);
470 assert(index < lengthof(_tree_layout_sprite));
472 const PalSpriteID *s = _tree_layout_sprite[index];
473 const TreePos *d = _tree_layout_xy[GB(tmp, 2, 2)];
475 /* combine trees into one sprite object */
476 StartSpriteCombine();
478 TreeListEnt te[4];
480 /* put the trees to draw in a list */
481 uint trees = GetTreeCount(ti->tile);
483 for (uint i = 0; i < trees; i++) {
484 SpriteID sprite = s[0].sprite + (i == trees - 1 ? GetTreeGrowth(ti->tile) : 3);
485 PaletteID pal = s[0].pal;
487 te[i].sprite = sprite;
488 te[i].pal = pal;
489 te[i].x = d->x;
490 te[i].y = d->y;
491 s++;
492 d++;
495 /* draw them in a sorted way */
496 byte z = ti->z + GetSlopeMaxZ(ti->tileh) / 2;
498 for (; trees > 0; trees--) {
499 uint min = te[0].x + te[0].y;
500 uint mi = 0;
502 for (uint i = 1; i < trees; i++) {
503 if ((uint)(te[i].x + te[i].y) < min) {
504 min = te[i].x + te[i].y;
505 mi = i;
509 AddSortableSpriteToDraw(te[mi].sprite, te[mi].pal, ti->x + te[mi].x, ti->y + te[mi].y, 16 - te[mi].x, 16 - te[mi].y, 0x30, z, IsTransparencySet(TO_TREES), -te[mi].x, -te[mi].y);
511 /* replace the removed one with the last one */
512 te[mi] = te[trees - 1];
515 EndSpriteCombine();
519 static uint GetSlopeZ_Trees(TileIndex tile, uint x, uint y)
521 uint z;
522 Slope tileh = GetTileSlope(tile, &z);
524 return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
527 static Foundation GetFoundation_Trees(TileIndex tile, Slope tileh)
529 return FOUNDATION_NONE;
532 static CommandCost ClearTile_Trees(TileIndex tile, DoCommandFlag flags)
534 uint num;
536 if (Company::IsValidID(_current_company)) {
537 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
538 if (t != NULL) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM, flags);
541 num = GetTreeCount(tile);
542 if (IsInsideMM(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;
544 if (flags & DC_EXEC) DoClearSquare(tile);
546 return CommandCost(EXPENSES_CONSTRUCTION, num * _price[PR_CLEAR_TREES]);
549 static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
551 TreeType tt = GetTreeType(tile);
553 if (IsInsideMM(tt, TREE_RAINFOREST, TREE_CACTUS)) {
554 td->str = STR_LAI_TREE_NAME_RAINFOREST;
555 } else {
556 td->str = tt == TREE_CACTUS ? STR_LAI_TREE_NAME_CACTUS_PLANTS : STR_LAI_TREE_NAME_TREES;
559 td->owner[0] = GetTileOwner(tile);
562 static void TileLoopTreesDesert(TileIndex tile)
564 switch (GetTropicZone(tile)) {
565 case TROPICZONE_DESERT:
566 if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
567 SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
568 MarkTileDirtyByTile(tile);
570 break;
572 case TROPICZONE_RAINFOREST: {
573 static const SoundFx forest_sounds[] = {
574 SND_42_LOON_BIRD,
575 SND_43_LION,
576 SND_44_MONKEYS,
577 SND_48_DISTANT_BIRD
579 uint32 r = Random();
581 if (Chance16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
582 break;
585 default: break;
589 static void TileLoopTreesAlps(TileIndex tile)
591 int k = GetTileZ(tile) - GetSnowLine() + TILE_HEIGHT;
593 if (k < 0) {
594 switch (GetTreeGround(tile)) {
595 case TREE_GROUND_SNOW_DESERT: SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3); break;
596 case TREE_GROUND_ROUGH_SNOW: SetTreeGroundDensity(tile, TREE_GROUND_ROUGH, 3); break;
597 default: return;
599 } else {
600 uint density = min((uint)k / TILE_HEIGHT, 3);
602 if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT && GetTreeGround(tile) != TREE_GROUND_ROUGH_SNOW) {
603 TreeGround tg = GetTreeGround(tile) == TREE_GROUND_ROUGH ? TREE_GROUND_ROUGH_SNOW : TREE_GROUND_SNOW_DESERT;
604 SetTreeGroundDensity(tile, tg, density);
605 } else if (GetTreeDensity(tile) != density) {
606 SetTreeGroundDensity(tile, GetTreeGround(tile), density);
607 } else {
608 if (GetTreeDensity(tile) == 3) {
609 uint32 r = Random();
610 if (Chance16I(1, 200, r)) {
611 SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
614 return;
617 MarkTileDirtyByTile(tile);
620 static void TileLoop_Trees(TileIndex tile)
622 if (GetTreeGround(tile) == TREE_GROUND_SHORE) {
623 TileLoop_Water(tile);
624 } else {
625 switch (_settings_game.game_creation.landscape) {
626 case LT_TROPIC: TileLoopTreesDesert(tile); break;
627 case LT_ARCTIC: TileLoopTreesAlps(tile); break;
631 TileLoopClearHelper(tile);
633 uint treeCounter = GetTreeCounter(tile);
635 /* Handle growth of grass (under trees/on MP_TREES tiles) at every 8th processings, like it's done for grass on MP_CLEAR tiles. */
636 if ((treeCounter & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
637 uint density = GetTreeDensity(tile);
638 if (density < 3) {
639 SetTreeGroundDensity(tile, TREE_GROUND_GRASS, density + 1);
640 MarkTileDirtyByTile(tile);
643 if (GetTreeCounter(tile) < 15) {
644 AddTreeCounter(tile, 1);
645 return;
647 SetTreeCounter(tile, 0);
649 switch (GetTreeGrowth(tile)) {
650 case 3: // regular sized tree
651 if (_settings_game.game_creation.landscape == LT_TROPIC &&
652 GetTreeType(tile) != TREE_CACTUS &&
653 GetTropicZone(tile) == TROPICZONE_DESERT) {
654 AddTreeGrowth(tile, 1);
655 } else {
656 switch (GB(Random(), 0, 3)) {
657 case 0: // start destructing
658 AddTreeGrowth(tile, 1);
659 break;
661 case 1: // add a tree
662 if (GetTreeCount(tile) < 4) {
663 AddTreeCount(tile, 1);
664 SetTreeGrowth(tile, 0);
665 break;
667 /* FALL THROUGH */
669 case 2: { // add a neighbouring tree
670 /* Don't plant extra trees if that's not allowed. */
671 if ((_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) == TROPICZONE_RAINFOREST) ?
672 _settings_game.construction.extra_tree_placement == ETP_NONE :
673 _settings_game.construction.extra_tree_placement != ETP_ALL) {
674 break;
677 TreeType treetype = GetTreeType(tile);
679 tile += TileOffsByDir((Direction)(Random() & 7));
681 /* Cacti don't spread */
682 if (!CanPlantTreesOnTile(tile, false)) return;
684 /* Don't plant trees, if ground was freshly cleared */
685 if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
687 PlantTreesOnTile(tile, treetype, 0, 0);
689 break;
692 default:
693 return;
696 break;
698 case 6: // final stage of tree destruction
699 if (GetTreeCount(tile) > 1) {
700 /* more than one tree, delete it */
701 AddTreeCount(tile, -1);
702 SetTreeGrowth(tile, 3);
703 } else {
704 /* just one tree, change type into MP_CLEAR */
705 switch (GetTreeGround(tile)) {
706 case TREE_GROUND_SHORE: MakeShore(tile); break;
707 case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, GetTreeDensity(tile)); break;
708 case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
709 case TREE_GROUND_ROUGH_SNOW: {
710 uint density = GetTreeDensity(tile);
711 MakeClear(tile, CLEAR_ROUGH, 3);
712 MakeSnow(tile, density);
713 break;
715 default: // snow or desert
716 if (_settings_game.game_creation.landscape == LT_TROPIC) {
717 MakeClear(tile, CLEAR_DESERT, GetTreeDensity(tile));
718 } else {
719 uint density = GetTreeDensity(tile);
720 MakeClear(tile, CLEAR_GRASS, 3);
721 MakeSnow(tile, density);
723 break;
726 break;
728 default:
729 AddTreeGrowth(tile, 1);
730 break;
733 MarkTileDirtyByTile(tile);
736 void OnTick_Trees()
738 /* Don't place trees if that's not allowed */
739 if (_settings_game.construction.extra_tree_placement == ETP_NONE) return;
741 uint32 r;
742 TileIndex tile;
743 TreeType tree;
745 /* place a tree at a random rainforest spot */
746 if (_settings_game.game_creation.landscape == LT_TROPIC &&
747 (r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
748 CanPlantTreesOnTile(tile, false) &&
749 (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
750 PlantTreesOnTile(tile, tree, 0, 0);
753 /* byte underflow */
754 if (--_trees_tick_ctr != 0 || _settings_game.construction.extra_tree_placement != ETP_ALL) return;
756 /* place a tree at a random spot */
757 r = Random();
758 tile = RandomTileSeed(r);
759 if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
760 PlantTreesOnTile(tile, tree, 0, 0);
764 static TrackStatus GetTileTrackStatus_Trees(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
766 return 0;
769 static void ChangeTileOwner_Trees(TileIndex tile, Owner old_owner, Owner new_owner)
771 /* not used */
774 void InitializeTrees()
776 _trees_tick_ctr = 0;
779 static CommandCost TerraformTile_Trees(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
781 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
785 extern const TileTypeProcs _tile_type_trees_procs = {
786 DrawTile_Trees, // draw_tile_proc
787 GetSlopeZ_Trees, // get_slope_z_proc
788 ClearTile_Trees, // clear_tile_proc
789 NULL, // add_accepted_cargo_proc
790 GetTileDesc_Trees, // get_tile_desc_proc
791 GetTileTrackStatus_Trees, // get_tile_track_status_proc
792 NULL, // click_tile_proc
793 NULL, // animate_tile_proc
794 TileLoop_Trees, // tile_loop_clear
795 ChangeTileOwner_Trees, // change_tile_owner_clear
796 NULL, // add_produced_cargo_proc
797 NULL, // vehicle_enter_tile_proc
798 GetFoundation_Trees, // get_foundation_proc
799 TerraformTile_Trees, // terraform_tile_proc