# tree_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "clear_map.h"
    00014 #include "landscape.h"
    00015 #include "tree_map.h"
    00016 #include "viewport_func.h"
    00017 #include "command_func.h"
    00018 #include "town.h"
    00019 #include "genworld.h"
    00020 #include "clear_func.h"
    00021 #include "company_func.h"
    00022 #include "sound_func.h"
    00023 #include "water.h"
    00024 #include "company_base.h"
    00025 #include "core/random_func.hpp"
    00026 #include "newgrf_generic.h"
    00027
    00028 #include "table/strings.h"
    00029 #include "table/tree_land.h"
    00030 #include "table/clear_land.h"
    00031
    00037 enum TreePlacer {
    00038   TP_NONE,
    00039   TP_ORIGINAL,
    00040   TP_IMPROVED,
    00041 };
    00042
    00044 enum ExtraTreePlacement {
    00045   ETP_NONE,
    00046   ETP_RAINFOREST,
    00047   ETP_ALL,
    00048 };
    00049
    00051 byte _trees_tick_ctr;
    00052
    00053 static const uint16 DEFAULT_TREE_STEPS = 1000;
    00054 static const uint16 DEFAULT_RAINFOREST_TREE_STEPS = 15000;
    00055 static const uint16 EDITOR_TREE_DIV = 5;
    00056
    00065 static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
    00066 {
    00067   switch (GetTileType(tile)) {
    00068     case MP_WATER:
    00069       return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile));
    00070
    00071     case MP_CLEAR:
    00072       return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && GetRawClearGround(tile) != CLEAR_ROCKS &&
    00073              (allow_desert || !IsClearGround(tile, CLEAR_DESERT));
    00074
    00075     default: return false;
    00076   }
    00077 }
    00078
    00090 static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, uint growth)
    00091 {
    00092   assert(treetype != TREE_INVALID);
    00093   assert(CanPlantTreesOnTile(tile, true));
    00094
    00095   TreeGround ground;
    00096   uint density = 3;
    00097
    00098   switch (GetTileType(tile)) {
    00099     case MP_WATER:
    00100       ground = TREE_GROUND_SHORE;
    00101       break;
    00102
    00103     case MP_CLEAR:
    00104       switch (GetClearGround(tile)) {
    00105         case CLEAR_GRASS:  ground = TREE_GROUND_GRASS;       break;
    00106         case CLEAR_ROUGH:  ground = TREE_GROUND_ROUGH;       break;
    00107         case CLEAR_SNOW:   ground = GetRawClearGround(tile) == CLEAR_ROUGH ? TREE_GROUND_ROUGH_SNOW : TREE_GROUND_SNOW_DESERT; break;
    00108         default:           ground = TREE_GROUND_SNOW_DESERT; break;
    00109       }
    00110       if (GetClearGround(tile) != CLEAR_ROUGH) density = GetClearDensity(tile);
    00111       break;
    00112
    00113     default: NOT_REACHED();
    00114   }
    00115
    00116   MakeTree(tile, treetype, count, growth, ground, density);
    00117 }
    00118
    00130 static TreeType GetRandomTreeType(TileIndex tile, uint seed)
    00131 {
    00132   switch (_settings_game.game_creation.landscape) {
    00133     case LT_TEMPERATE:
    00134       return (TreeType)(seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE);
    00135
    00136     case LT_ARCTIC:
    00137       return (TreeType)(seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC);
    00138
    00139     case LT_TROPIC:
    00140       switch (GetTropicZone(tile)) {
    00141         case TROPICZONE_NORMAL:  return (TreeType)(seed * TREE_COUNT_SUB_TROPICAL / 256 + TREE_SUB_TROPICAL);
    00142         case TROPICZONE_DESERT:  return (TreeType)((seed > 12) ? TREE_INVALID : TREE_CACTUS);
    00143         default:                 return (TreeType)(seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST);
    00144       }
    00145
    00146     default:
    00147       return (TreeType)(seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND);
    00148   }
    00149 }
    00150
    00160 static void PlaceTree(TileIndex tile, uint32 r)
    00161 {
    00162   TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
    00163
    00164   if (tree != TREE_INVALID) {
    00165     PlantTreesOnTile(tile, tree, GB(r, 22, 2), min(GB(r, 16, 3), 6));
    00166
    00167     /* Rerandomize ground, if neither snow nor shore */
    00168     TreeGround ground = GetTreeGround(tile);
    00169     if (ground != TREE_GROUND_SNOW_DESERT && ground != TREE_GROUND_ROUGH_SNOW && ground != TREE_GROUND_SHORE) {
    00170       SetTreeGroundDensity(tile, (TreeGround)GB(r, 28, 1), 3);
    00171     }
    00172
    00173     /* Set the counter to a random start value */
    00174     SetTreeCounter(tile, (TreeGround)GB(r, 24, 4));
    00175   }
    00176 }
    00177
    00184 static void PlaceTreeGroups(uint num_groups)
    00185 {
    00186   do {
    00187     TileIndex center_tile = RandomTile();
    00188
    00189     for (uint i = 0; i < DEFAULT_TREE_STEPS; i++) {
    00190       uint32 r = Random();
    00191       int x = GB(r, 0, 5) - 16;
    00192       int y = GB(r, 8, 5) - 16;
    00193       uint dist = abs(x) + abs(y);
    00194       TileIndex cur_tile = TileAddWrap(center_tile, x, y);
    00195
    00196       IncreaseGeneratingWorldProgress(GWP_TREE);
    00197
    00198       if (cur_tile != INVALID_TILE && dist <= 13 && CanPlantTreesOnTile(cur_tile, true)) {
    00199         PlaceTree(cur_tile, r);
    00200       }
    00201     }
    00202
    00203   } while (--num_groups);
    00204 }
    00205
    00215 static void PlaceTreeAtSameHeight(TileIndex tile, int height)
    00216 {
    00217   for (uint i = 0; i < DEFAULT_TREE_STEPS; i++) {
    00218     uint32 r = Random();
    00219     int x = GB(r, 0, 5) - 16;
    00220     int y = GB(r, 8, 5) - 16;
    00221     TileIndex cur_tile = TileAddWrap(tile, x, y);
    00222     if (cur_tile == INVALID_TILE) continue;
    00223
    00224     /* Keep in range of the existing tree */
    00225     if (abs(x) + abs(y) > 16) continue;
    00226
    00227     /* Clear tile, no farm-tiles or rocks */
    00228     if (!CanPlantTreesOnTile(cur_tile, true)) continue;
    00229
    00230     /* Not too much height difference */
    00231     if (Delta(GetTileZ(cur_tile), height) > 2) continue;
    00232
    00233     /* Place one tree and quit */
    00234     PlaceTree(cur_tile, r);
    00235     break;
    00236   }
    00237 }
    00238
    00244 void PlaceTreesRandomly()
    00245 {
    00246   int i, j, ht;
    00247
    00248   i = ScaleByMapSize(DEFAULT_TREE_STEPS);
    00249   if (_game_mode == GM_EDITOR) i /= EDITOR_TREE_DIV;
    00250   do {
    00251     uint32 r = Random();
    00252     TileIndex tile = RandomTileSeed(r);
    00253
    00254     IncreaseGeneratingWorldProgress(GWP_TREE);
    00255
    00256     if (CanPlantTreesOnTile(tile, true)) {
    00257       PlaceTree(tile, r);
    00258       if (_settings_game.game_creation.tree_placer != TP_IMPROVED) continue;
    00259
    00260       /* Place a number of trees based on the tile height.
    00261        *  This gives a cool effect of multiple trees close together.
    00262        *  It is almost real life ;) */
    00263       ht = GetTileZ(tile);
    00264       /* The higher we get, the more trees we plant */
    00265       j = GetTileZ(tile) * 2;
    00266       /* Above snowline more trees! */
    00267       if (_settings_game.game_creation.landscape == LT_ARCTIC && ht > GetSnowLine()) j *= 3;
    00268       while (j--) {
    00269         PlaceTreeAtSameHeight(tile, ht);
    00270       }
    00271     }
    00272   } while (--i);
    00273
    00274   /* place extra trees at rainforest area */
    00275   if (_settings_game.game_creation.landscape == LT_TROPIC) {
    00276     i = ScaleByMapSize(DEFAULT_RAINFOREST_TREE_STEPS);
    00277     if (_game_mode == GM_EDITOR) i /= EDITOR_TREE_DIV;
    00278
    00279     do {
    00280       uint32 r = Random();
    00281       TileIndex tile = RandomTileSeed(r);
    00282
    00283       IncreaseGeneratingWorldProgress(GWP_TREE);
    00284
    00285       if (GetTropicZone(tile) == TROPICZONE_RAINFOREST && CanPlantTreesOnTile(tile, false)) {
    00286         PlaceTree(tile, r);
    00287       }
    00288     } while (--i);
    00289   }
    00290 }
    00291
    00298 void GenerateTrees()
    00299 {
    00300   uint i, total;
    00301
    00302   if (_settings_game.game_creation.tree_placer == TP_NONE) return;
    00303
    00304   switch (_settings_game.game_creation.tree_placer) {
    00305     case TP_ORIGINAL: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 15 : 6; break;
    00306     case TP_IMPROVED: i = _settings_game.game_creation.landscape == LT_ARCTIC ?  4 : 2; break;
    00307     default: NOT_REACHED();
    00308   }
    00309
    00310   total = ScaleByMapSize(DEFAULT_TREE_STEPS);
    00311   if (_settings_game.game_creation.landscape == LT_TROPIC) total += ScaleByMapSize(DEFAULT_RAINFOREST_TREE_STEPS);
    00312   total *= i;
    00313   uint num_groups = (_settings_game.game_creation.landscape != LT_TOYLAND) ? ScaleByMapSize(GB(Random(), 0, 5) + 25) : 0;
    00314   total += num_groups * DEFAULT_TREE_STEPS;
    00315   SetGeneratingWorldProgress(GWP_TREE, total);
    00316
    00317   if (num_groups != 0) PlaceTreeGroups(num_groups);
    00318
    00319   for (; i != 0; i--) {
    00320     PlaceTreesRandomly();
    00321   }
    00322 }
    00323
    00333 CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00334 {
    00335   StringID msg = INVALID_STRING_ID;
    00336   CommandCost cost(EXPENSES_OTHER);
    00337   const byte tree_to_plant = GB(p1, 0, 8); // We cannot use Extract as min and max are climate specific.
    00338
    00339   if (p2 >= MapSize()) return CMD_ERROR;
    00340   /* Check the tree type within the current climate */
    00341   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;
    00342
    00343   Company *c = (_game_mode != GM_EDITOR) ? Company::GetIfValid(_current_company) : NULL;
    00344   int limit = (c == NULL ? INT32_MAX : GB(c->tree_limit, 16, 16));
    00345
    00346   TileArea ta(tile, p2);
    00347   TILE_AREA_LOOP(tile, ta) {
    00348     switch (GetTileType(tile)) {
    00349       case MP_TREES:
    00350         /* no more space for trees? */
    00351         if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 4) {
    00352           msg = STR_ERROR_TREE_ALREADY_HERE;
    00353           continue;
    00354         }
    00355
    00356         /* Test tree limit. */
    00357         if (--limit < 1) {
    00358           msg = STR_ERROR_TREE_PLANT_LIMIT_REACHED;
    00359           break;
    00360         }
    00361
    00362         if (flags & DC_EXEC) {
    00363           AddTreeCount(tile, 1);
    00364           MarkTileDirtyByTile(tile);
    00365           if (c != NULL) c->tree_limit -= 1 << 16;
    00366         }
    00367         /* 2x as expensive to add more trees to an existing tile */
    00368         cost.AddCost(_price[PR_BUILD_TREES] * 2);
    00369         break;
    00370
    00371       case MP_WATER:
    00372         if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile))) {
    00373           msg = STR_ERROR_CAN_T_BUILD_ON_WATER;
    00374           continue;
    00375         }
    00376         /* FALL THROUGH */
    00377       case MP_CLEAR: {
    00378         if (IsBridgeAbove(tile)) {
    00379           msg = STR_ERROR_SITE_UNSUITABLE;
    00380           continue;
    00381         }
    00382
    00383         TreeType treetype = (TreeType)tree_to_plant;
    00384         /* Be a bit picky about which trees go where. */
    00385         if (_settings_game.game_creation.landscape == LT_TROPIC && treetype != TREE_INVALID && (
    00386             /* No cacti outside the desert */
    00387             (treetype == TREE_CACTUS && GetTropicZone(tile) != TROPICZONE_DESERT) ||
    00388             /* No rain forest trees outside the rain forest, except in the editor mode where it makes those tiles rain forest tile */
    00389             (IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS) && GetTropicZone(tile) != TROPICZONE_RAINFOREST && _game_mode != GM_EDITOR) ||
    00390             /* And no subtropical trees in the desert/rain forest */
    00391             (IsInsideMM(treetype, TREE_SUB_TROPICAL, TREE_TOYLAND) && GetTropicZone(tile) != TROPICZONE_NORMAL))) {
    00392           msg = STR_ERROR_TREE_WRONG_TERRAIN_FOR_TREE_TYPE;
    00393           continue;
    00394         }
    00395
    00396         /* Test tree limit. */
    00397         if (--limit < 1) {
    00398           msg = STR_ERROR_TREE_PLANT_LIMIT_REACHED;
    00399           break;
    00400         }
    00401
    00402         if (IsTileType(tile, MP_CLEAR)) {
    00403           /* Remove fields or rocks. Note that the ground will get barrened */
    00404           switch (GetRawClearGround(tile)) {
    00405             case CLEAR_FIELDS:
    00406             case CLEAR_ROCKS: {
    00407               CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00408               if (ret.Failed()) return ret;
    00409               cost.AddCost(ret);
    00410               break;
    00411             }
    00412
    00413             default: break;
    00414           }
    00415         }
    00416
    00417         if (_game_mode != GM_EDITOR && Company::IsValidID(_current_company)) {
    00418           Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
    00419           if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
    00420         }
    00421
    00422         if (flags & DC_EXEC) {
    00423           if (treetype == TREE_INVALID) {
    00424             treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
    00425             if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
    00426           }
    00427
    00428           /* Plant full grown trees in scenario editor */
    00429           PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
    00430           MarkTileDirtyByTile(tile);
    00431           if (c != NULL) c->tree_limit -= 1 << 16;
    00432
    00433           /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
    00434           if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS)) {
    00435             SetTropicZone(tile, TROPICZONE_RAINFOREST);
    00436           }
    00437         }
    00438         cost.AddCost(_price[PR_BUILD_TREES]);
    00439         break;
    00440       }
    00441
    00442       default:
    00443         msg = STR_ERROR_SITE_UNSUITABLE;
    00444         break;
    00445     }
    00446
    00447     /* Tree limit used up? No need to check more. */
    00448     if (limit < 0) break;
    00449   }
    00450
    00451   if (cost.GetCost() == 0) {
    00452     return_cmd_error(msg);
    00453   } else {
    00454     return cost;
    00455   }
    00456 }
    00457
    00458 struct TreeListEnt : PalSpriteID {
    00459   byte x, y;
    00460 };
    00461
    00462 static void DrawTile_Trees(TileInfo *ti)
    00463 {
    00464   switch (GetTreeGround(ti->tile)) {
    00465     case TREE_GROUND_SHORE: DrawShoreTile(ti->tileh); break;
    00466     case TREE_GROUND_GRASS: DrawClearLandTile(ti, GetTreeDensity(ti->tile)); break;
    00467     case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
    00468     default: DrawGroundSprite(_clear_land_sprites_snow_desert[GetTreeDensity(ti->tile)] + SlopeToSpriteOffset(ti->tileh), PAL_NONE); break;
    00469   }
    00470
    00471   /* Do not draw trees when the invisible trees setting is set */
    00472   if (IsInvisibilitySet(TO_TREES)) return;
    00473
    00474   uint tmp = CountBits(ti->tile + ti->x + ti->y);
    00475   uint index = GB(tmp, 0, 2) + (GetTreeType(ti->tile) << 2);
    00476
    00477   /* different tree styles above one of the grounds */
    00478   if ((GetTreeGround(ti->tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(ti->tile) == TREE_GROUND_ROUGH_SNOW) &&
    00479       GetTreeDensity(ti->tile) >= 2 &&
    00480       IsInsideMM(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
    00481     index += 164 - (TREE_SUB_ARCTIC << 2);
    00482   }
    00483
    00484   assert(index < lengthof(_tree_layout_sprite));
    00485
    00486   const PalSpriteID *s = _tree_layout_sprite[index];
    00487   const TreePos *d = _tree_layout_xy[GB(tmp, 2, 2)];
    00488
    00489   /* combine trees into one sprite object */
    00490   StartSpriteCombine();
    00491
    00492   TreeListEnt te[4];
    00493
    00494   /* put the trees to draw in a list */
    00495   uint trees = GetTreeCount(ti->tile);
    00496
    00497   for (uint i = 0; i < trees; i++) {
    00498     SpriteID sprite = s[0].sprite + (i == trees - 1 ? GetTreeGrowth(ti->tile) : 3);
    00499     PaletteID pal = s[0].pal;
    00500
    00501     te[i].sprite = sprite;
    00502     te[i].pal    = pal;
    00503     te[i].x = d->x;
    00504     te[i].y = d->y;
    00505     s++;
    00506     d++;
    00507   }
    00508
    00509   /* draw them in a sorted way */
    00510   int z = ti->z + GetSlopeMaxPixelZ(ti->tileh) / 2;
    00511
    00512   for (; trees > 0; trees--) {
    00513     uint min = te[0].x + te[0].y;
    00514     uint mi = 0;
    00515
    00516     for (uint i = 1; i < trees; i++) {
    00517       if ((uint)(te[i].x + te[i].y) < min) {
    00518         min = te[i].x + te[i].y;
    00519         mi = i;
    00520       }
    00521     }
    00522
    00523     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);
    00524
    00525     /* replace the removed one with the last one */
    00526     te[mi] = te[trees - 1];
    00527   }
    00528
    00529   EndSpriteCombine();
    00530 }
    00531
    00532
    00533 static int GetSlopePixelZ_Trees(TileIndex tile, uint x, uint y)
    00534 {
    00535   int z;
    00536   Slope tileh = GetTilePixelSlope(tile, &z);
    00537
    00538   return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
    00539 }
    00540
    00541 static Foundation GetFoundation_Trees(TileIndex tile, Slope tileh)
    00542 {
    00543   return FOUNDATION_NONE;
    00544 }
    00545
    00546 static CommandCost ClearTile_Trees(TileIndex tile, DoCommandFlag flags)
    00547 {
    00548   uint num;
    00549
    00550   if (Company::IsValidID(_current_company)) {
    00551     Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
    00552     if (t != NULL) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM, flags);
    00553   }
    00554
    00555   num = GetTreeCount(tile);
    00556   if (IsInsideMM(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;
    00557
    00558   if (flags & DC_EXEC) DoClearSquare(tile);
    00559
    00560   return CommandCost(EXPENSES_CONSTRUCTION, num * _price[PR_CLEAR_TREES]);
    00561 }
    00562
    00563 static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
    00564 {
    00565   TreeType tt = GetTreeType(tile);
    00566
    00567   if (IsInsideMM(tt, TREE_RAINFOREST, TREE_CACTUS)) {
    00568     td->str = STR_LAI_TREE_NAME_RAINFOREST;
    00569   } else {
    00570     td->str = tt == TREE_CACTUS ? STR_LAI_TREE_NAME_CACTUS_PLANTS : STR_LAI_TREE_NAME_TREES;
    00571   }
    00572
    00573   td->owner[0] = GetTileOwner(tile);
    00574 }
    00575
    00576 static void TileLoopTreesDesert(TileIndex tile)
    00577 {
    00578   switch (GetTropicZone(tile)) {
    00579     case TROPICZONE_DESERT:
    00580       if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
    00581         SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
    00582         MarkTileDirtyByTile(tile);
    00583       }
    00584       break;
    00585
    00586     case TROPICZONE_RAINFOREST: {
    00587       static const SoundFx forest_sounds[] = {
    00588         SND_42_LOON_BIRD,
    00589         SND_43_LION,
    00590         SND_44_MONKEYS,
    00591         SND_48_DISTANT_BIRD
    00592       };
    00593       uint32 r = Random();
    00594
    00595       if (Chance16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
    00596       break;
    00597     }
    00598
    00599     default: break;
    00600   }
    00601 }
    00602
    00603 static void TileLoopTreesAlps(TileIndex tile)
    00604 {
    00605   int k = GetTileZ(tile) - GetSnowLine() + 1;
    00606
    00607   if (k < 0) {
    00608     switch (GetTreeGround(tile)) {
    00609       case TREE_GROUND_SNOW_DESERT: SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3); break;
    00610       case TREE_GROUND_ROUGH_SNOW:  SetTreeGroundDensity(tile, TREE_GROUND_ROUGH, 3); break;
    00611       default: return;
    00612     }
    00613   } else {
    00614     uint density = min<uint>(k, 3);
    00615
    00616     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT && GetTreeGround(tile) != TREE_GROUND_ROUGH_SNOW) {
    00617       TreeGround tg = GetTreeGround(tile) == TREE_GROUND_ROUGH ? TREE_GROUND_ROUGH_SNOW : TREE_GROUND_SNOW_DESERT;
    00618       SetTreeGroundDensity(tile, tg, density);
    00619     } else if (GetTreeDensity(tile) != density) {
    00620       SetTreeGroundDensity(tile, GetTreeGround(tile), density);
    00621     } else {
    00622       if (GetTreeDensity(tile) == 3) {
    00623         uint32 r = Random();
    00624         if (Chance16I(1, 200, r)) {
    00625           SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
    00626         }
    00627       }
    00628       return;
    00629     }
    00630   }
    00631   MarkTileDirtyByTile(tile);
    00632 }
    00633
    00634 static void TileLoop_Trees(TileIndex tile)
    00635 {
    00636   if (GetTreeGround(tile) == TREE_GROUND_SHORE) {
    00637     TileLoop_Water(tile);
    00638   } else {
    00639     switch (_settings_game.game_creation.landscape) {
    00640       case LT_TROPIC: TileLoopTreesDesert(tile); break;
    00641       case LT_ARCTIC: TileLoopTreesAlps(tile);   break;
    00642     }
    00643   }
    00644
    00645   AmbientSoundEffect(tile);
    00646
    00647   uint treeCounter = GetTreeCounter(tile);
    00648
    00649   /* Handle growth of grass (under trees/on MP_TREES tiles) at every 8th processings, like it's done for grass on MP_CLEAR tiles. */
    00650   if ((treeCounter & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
    00651     uint density = GetTreeDensity(tile);
    00652     if (density < 3) {
    00653       SetTreeGroundDensity(tile, TREE_GROUND_GRASS, density + 1);
    00654       MarkTileDirtyByTile(tile);
    00655     }
    00656   }
    00657   if (GetTreeCounter(tile) < 15) {
    00658     AddTreeCounter(tile, 1);
    00659     return;
    00660   }
    00661   SetTreeCounter(tile, 0);
    00662
    00663   switch (GetTreeGrowth(tile)) {
    00664     case 3: // regular sized tree
    00665       if (_settings_game.game_creation.landscape == LT_TROPIC &&
    00666           GetTreeType(tile) != TREE_CACTUS &&
    00667           GetTropicZone(tile) == TROPICZONE_DESERT) {
    00668         AddTreeGrowth(tile, 1);
    00669       } else {
    00670         switch (GB(Random(), 0, 3)) {
    00671           case 0: // start destructing
    00672             AddTreeGrowth(tile, 1);
    00673             break;
    00674
    00675           case 1: // add a tree
    00676             if (GetTreeCount(tile) < 4) {
    00677               AddTreeCount(tile, 1);
    00678               SetTreeGrowth(tile, 0);
    00679               break;
    00680             }
    00681             /* FALL THROUGH */
    00682
    00683           case 2: { // add a neighbouring tree
    00684             /* Don't plant extra trees if that's not allowed. */
    00685             if ((_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) == TROPICZONE_RAINFOREST) ?
    00686                 _settings_game.construction.extra_tree_placement == ETP_NONE :
    00687                 _settings_game.construction.extra_tree_placement != ETP_ALL) {
    00688               break;
    00689             }
    00690
    00691             TreeType treetype = GetTreeType(tile);
    00692
    00693             tile += TileOffsByDir((Direction)(Random() & 7));
    00694
    00695             /* Cacti don't spread */
    00696             if (!CanPlantTreesOnTile(tile, false)) return;
    00697
    00698             /* Don't plant trees, if ground was freshly cleared */
    00699             if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
    00700
    00701             PlantTreesOnTile(tile, treetype, 0, 0);
    00702
    00703             break;
    00704           }
    00705
    00706           default:
    00707             return;
    00708         }
    00709       }
    00710       break;
    00711
    00712     case 6: // final stage of tree destruction
    00713       if (GetTreeCount(tile) > 1) {
    00714         /* more than one tree, delete it */
    00715         AddTreeCount(tile, -1);
    00716         SetTreeGrowth(tile, 3);
    00717       } else {
    00718         /* just one tree, change type into MP_CLEAR */
    00719         switch (GetTreeGround(tile)) {
    00720           case TREE_GROUND_SHORE: MakeShore(tile); break;
    00721           case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, GetTreeDensity(tile)); break;
    00722           case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
    00723           case TREE_GROUND_ROUGH_SNOW: {
    00724             uint density = GetTreeDensity(tile);
    00725             MakeClear(tile, CLEAR_ROUGH, 3);
    00726             MakeSnow(tile, density);
    00727             break;
    00728           }
    00729           default: // snow or desert
    00730             if (_settings_game.game_creation.landscape == LT_TROPIC) {
    00731               MakeClear(tile, CLEAR_DESERT, GetTreeDensity(tile));
    00732             } else {
    00733               uint density = GetTreeDensity(tile);
    00734               MakeClear(tile, CLEAR_GRASS, 3);
    00735               MakeSnow(tile, density);
    00736             }
    00737             break;
    00738         }
    00739       }
    00740       break;
    00741
    00742     default:
    00743       AddTreeGrowth(tile, 1);
    00744       break;
    00745   }
    00746
    00747   MarkTileDirtyByTile(tile);
    00748 }
    00749
    00750 void OnTick_Trees()
    00751 {
    00752   /* Don't place trees if that's not allowed */
    00753   if (_settings_game.construction.extra_tree_placement == ETP_NONE) return;
    00754
    00755   uint32 r;
    00756   TileIndex tile;
    00757   TreeType tree;
    00758
    00759   /* place a tree at a random rainforest spot */
    00760   if (_settings_game.game_creation.landscape == LT_TROPIC &&
    00761       (r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
    00762       CanPlantTreesOnTile(tile, false) &&
    00763       (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
    00764     PlantTreesOnTile(tile, tree, 0, 0);
    00765   }
    00766
    00767   /* byte underflow */
    00768   if (--_trees_tick_ctr != 0 || _settings_game.construction.extra_tree_placement != ETP_ALL) return;
    00769
    00770   /* place a tree at a random spot */
    00771   r = Random();
    00772   tile = RandomTileSeed(r);
    00773   if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
    00774     PlantTreesOnTile(tile, tree, 0, 0);
    00775   }
    00776 }
    00777
    00778 static TrackStatus GetTileTrackStatus_Trees(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    00779 {
    00780   return 0;
    00781 }
    00782
    00783 static void ChangeTileOwner_Trees(TileIndex tile, Owner old_owner, Owner new_owner)
    00784 {
    00785   /* not used */
    00786 }
    00787
    00788 void InitializeTrees()
    00789 {
    00790   _trees_tick_ctr = 0;
    00791 }
    00792
    00793 static CommandCost TerraformTile_Trees(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
    00794 {
    00795   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00796 }
    00797
    00798
    00799 extern const TileTypeProcs _tile_type_trees_procs = {
    00800   DrawTile_Trees,           // draw_tile_proc
    00801   GetSlopePixelZ_Trees,     // get_slope_z_proc
    00802   ClearTile_Trees,          // clear_tile_proc
    00803   NULL,                     // add_accepted_cargo_proc
    00804   GetTileDesc_Trees,        // get_tile_desc_proc
    00805   GetTileTrackStatus_Trees, // get_tile_track_status_proc
    00806   NULL,                     // click_tile_proc
    00807   NULL,                     // animate_tile_proc
    00808   TileLoop_Trees,           // tile_loop_proc
    00809   ChangeTileOwner_Trees,    // change_tile_owner_proc
    00810   NULL,                     // add_produced_cargo_proc
    00811   NULL,                     // vehicle_enter_tile_proc
    00812   GetFoundation_Trees,      // get_foundation_proc
    00813   TerraformTile_Trees,      // terraform_tile_proc
    00814 };