# tree_cmd.cpp

Go to the documentation of this file.

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