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