# tree_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: tree_cmd.cpp 16744 2009-07-04 21:06:17Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "clear_map.h"
    00008 #include "landscape.h"
    00009 #include "tree_map.h"
    00010 #include "viewport_func.h"
    00011 #include "command_func.h"
    00012 #include "economy_func.h"
    00013 #include "town.h"
    00014 #include "variables.h"
    00015 #include "genworld.h"
    00016 #include "transparency.h"
    00017 #include "functions.h"
    00018 #include "company_func.h"
    00019 #include "sound_func.h"
    00020 #include "water_map.h"
    00021 #include "water.h"
    00022 #include "landscape_type.h"
    00023 #include "company_base.h"
    00024
    00025 #include "table/strings.h"
    00026 #include "table/sprites.h"
    00027 #include "table/tree_land.h"
    00028
    00034 enum TreePlacer {
    00035   TP_NONE,
    00036   TP_ORIGINAL,
    00037   TP_IMPROVED,
    00038 };
    00039
    00048 static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
    00049 {
    00050   switch (GetTileType(tile)) {
    00051     case MP_WATER:
    00052       return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL));
    00053
    00054     case MP_CLEAR:
    00055       return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && !IsClearGround(tile, CLEAR_ROCKS) &&
    00056              (allow_desert || !IsClearGround(tile, CLEAR_DESERT));
    00057
    00058     default: return false;
    00059   }
    00060 }
    00061
    00073 static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, uint growth)
    00074 {
    00075   assert(treetype != TREE_INVALID);
    00076   assert(CanPlantTreesOnTile(tile, true));
    00077
    00078   TreeGround ground;
    00079   uint density = 3;
    00080
    00081   switch (GetTileType(tile)) {
    00082     case MP_WATER:
    00083       ground = TREE_GROUND_SHORE;
    00084       break;
    00085
    00086     case MP_CLEAR:
    00087       switch (GetClearGround(tile)) {
    00088         case CLEAR_GRASS:  ground = TREE_GROUND_GRASS;       density = GetClearDensity(tile); break;
    00089         case CLEAR_ROUGH:  ground = TREE_GROUND_ROUGH;                                        break;
    00090         default:           ground = TREE_GROUND_SNOW_DESERT; density = GetClearDensity(tile); break;
    00091       }
    00092       break;
    00093
    00094     default: NOT_REACHED();
    00095   }
    00096
    00097   MakeTree(tile, treetype, count, growth, ground, density);
    00098 }
    00099
    00111 static TreeType GetRandomTreeType(TileIndex tile, uint seed)
    00112 {
    00113   switch (_settings_game.game_creation.landscape) {
    00114     case LT_TEMPERATE:
    00115       return (TreeType)(seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE);
    00116
    00117     case LT_ARCTIC:
    00118       return (TreeType)(seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC);
    00119
    00120     case LT_TROPIC:
    00121       switch (GetTropicZone(tile)) {
    00122         case TROPICZONE_NORMAL:  return (TreeType)(seed * TREE_COUNT_SUB_TROPICAL / 256 + TREE_SUB_TROPICAL);
    00123         case TROPICZONE_DESERT:  return (TreeType)((seed > 12) ? TREE_INVALID : TREE_CACTUS);
    00124         default:                 return (TreeType)(seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST);
    00125       }
    00126
    00127     default:
    00128       return (TreeType)(seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND);
    00129   }
    00130 }
    00131
    00141 static void PlaceTree(TileIndex tile, uint32 r)
    00142 {
    00143   TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
    00144
    00145   if (tree != TREE_INVALID) {
    00146     PlantTreesOnTile(tile, tree, GB(r, 22, 2), min(GB(r, 16, 3), 6));
    00147
    00148     /* Rerandomize ground, if neither snow nor shore */
    00149     TreeGround ground = GetTreeGround(tile);
    00150     if (ground != TREE_GROUND_SNOW_DESERT && ground != TREE_GROUND_SHORE) {
    00151       SetTreeGroundDensity(tile, (TreeGround)GB(r, 28, 1), 3);
    00152     }
    00153
    00154     /* Set the counter to a random start value */
    00155     SetTreeCounter(tile, (TreeGround)GB(r, 24, 4));
    00156   }
    00157 }
    00158
    00168 static void DoPlaceMoreTrees(TileIndex tile)
    00169 {
    00170   uint i;
    00171
    00172   for (i = 0; i < 1000; i++) {
    00173     uint32 r = Random();
    00174     int x = GB(r, 0, 5) - 16;
    00175     int y = GB(r, 8, 5) - 16;
    00176     uint dist = abs(x) + abs(y);
    00177     TileIndex cur_tile = TileAddWrap(tile, x, y);
    00178
    00179     if (cur_tile != INVALID_TILE && dist <= 13 && CanPlantTreesOnTile(cur_tile, true)) {
    00180       PlaceTree(cur_tile, r);
    00181     }
    00182   }
    00183 }
    00184
    00190 static void PlaceMoreTrees()
    00191 {
    00192   uint i = ScaleByMapSize(GB(Random(), 0, 5) + 25);
    00193   do {
    00194     DoPlaceMoreTrees(RandomTile());
    00195   } while (--i);
    00196 }
    00197
    00207 static void PlaceTreeAtSameHeight(TileIndex tile, uint height)
    00208 {
    00209   uint i;
    00210
    00211   for (i = 0; i < 1000; i++) {
    00212     uint32 r = Random();
    00213     int x = GB(r, 0, 5) - 16;
    00214     int y = GB(r, 8, 5) - 16;
    00215     TileIndex cur_tile = TileAddWrap(tile, x, y);
    00216     if (cur_tile == INVALID_TILE) continue;
    00217
    00218     /* Keep in range of the existing tree */
    00219     if (abs(x) + abs(y) > 16) continue;
    00220
    00221     /* Clear tile, no farm-tiles or rocks */
    00222     if (!CanPlantTreesOnTile(cur_tile, true)) continue;
    00223
    00224     /* Not too much height difference */
    00225     if (Delta(GetTileZ(cur_tile), height) > 2) continue;
    00226
    00227     /* Place one tree and quit */
    00228     PlaceTree(cur_tile, r);
    00229     break;
    00230   }
    00231 }
    00232
    00238 void PlaceTreesRandomly()
    00239 {
    00240   uint i, j, ht;
    00241
    00242   i = ScaleByMapSize(1000);
    00243   do {
    00244     uint32 r = Random();
    00245     TileIndex tile = RandomTileSeed(r);
    00246
    00247     IncreaseGeneratingWorldProgress(GWP_TREE);
    00248
    00249     if (CanPlantTreesOnTile(tile, true)) {
    00250       PlaceTree(tile, r);
    00251       if (_settings_game.game_creation.tree_placer != TP_IMPROVED) continue;
    00252
    00253       /* Place a number of trees based on the tile height.
    00254        *  This gives a cool effect of multiple trees close together.
    00255        *  It is almost real life ;) */
    00256       ht = GetTileZ(tile);
    00257       /* The higher we get, the more trees we plant */
    00258       j = GetTileZ(tile) / TILE_HEIGHT * 2;
    00259       while (j--) {
    00260         /* Above snowline more trees! */
    00261         if (_settings_game.game_creation.landscape == LT_ARCTIC && ht > GetSnowLine()) {
    00262           PlaceTreeAtSameHeight(tile, ht);
    00263           PlaceTreeAtSameHeight(tile, ht);
    00264         };
    00265
    00266         PlaceTreeAtSameHeight(tile, ht);
    00267       }
    00268     }
    00269   } while (--i);
    00270
    00271   /* place extra trees at rainforest area */
    00272   if (_settings_game.game_creation.landscape == LT_TROPIC) {
    00273     i = ScaleByMapSize(15000);
    00274
    00275     do {
    00276       uint32 r = Random();
    00277       TileIndex tile = RandomTileSeed(r);
    00278
    00279       IncreaseGeneratingWorldProgress(GWP_TREE);
    00280
    00281       if (GetTropicZone(tile) == TROPICZONE_RAINFOREST && CanPlantTreesOnTile(tile, false)) {
    00282         PlaceTree(tile, r);
    00283       }
    00284     } while (--i);
    00285   }
    00286 }
    00287
    00294 void GenerateTrees()
    00295 {
    00296   uint i, total;
    00297
    00298   if (_settings_game.game_creation.tree_placer == TP_NONE) return;
    00299
    00300   if (_settings_game.game_creation.landscape != LT_TOYLAND) PlaceMoreTrees();
    00301
    00302   switch (_settings_game.game_creation.tree_placer) {
    00303     case TP_ORIGINAL: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 15 : 6; break;
    00304     case TP_IMPROVED: i = _settings_game.game_creation.landscape == LT_ARCTIC ?  4 : 2; break;
    00305     default: NOT_REACHED(); return;
    00306   }
    00307
    00308   total = ScaleByMapSize(1000);
    00309   if (_settings_game.game_creation.landscape == LT_TROPIC) total += ScaleByMapSize(15000);
    00310   total *= i;
    00311   SetGeneratingWorldProgress(GWP_TREE, total);
    00312
    00313   for (; i != 0; i--) {
    00314     PlaceTreesRandomly();
    00315   }
    00316 }
    00317
    00324 CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00325 {
    00326   StringID msg = INVALID_STRING_ID;
    00327   CommandCost cost(EXPENSES_OTHER);
    00328   int ex;
    00329   int ey;
    00330   int sx, sy, x, y;
    00331
    00332   if (p2 >= MapSize()) return CMD_ERROR;
    00333   /* Check the tree type. It can be random or some valid value within the current climate */
    00334   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;
    00335
    00336   /* make sure sx,sy are smaller than ex, ey */
    00337   ex = TileX(tile);
    00338   ey = TileY(tile);
    00339   sx = TileX(p2);
    00340   sy = TileY(p2);
    00341   if (ex < sx) Swap(ex, sx);
    00342   if (ey < sy) Swap(ey, sy);
    00343
    00344   for (x = sx; x <= ex; x++) {
    00345     for (y = sy; y <= ey; y++) {
    00346       TileIndex tile = TileXY(x, y);
    00347
    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_2803_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.build_trees * 2);
    00362           break;
    00363
    00364         case MP_WATER:
    00365           if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL))) {
    00366             msg = STR_3807_CAN_T_BUILD_ON_WATER;
    00367             continue;
    00368           }
    00369         /* FALL THROUGH */
    00370         case MP_CLEAR:
    00371           if (IsBridgeAbove(tile)) {
    00372             msg = STR_2804_SITE_UNSUITABLE;
    00373             continue;
    00374           }
    00375
    00376           if (IsTileType(tile, MP_CLEAR)) {
    00377             /* Remove fields or rocks. Note that the ground will get barrened */
    00378             switch (GetClearGround(tile)) {
    00379               case CLEAR_FIELDS:
    00380               case CLEAR_ROCKS: {
    00381                 CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00382                 if (CmdFailed(ret)) return ret;
    00383                 cost.AddCost(ret);
    00384                 break;
    00385               }
    00386
    00387               default: break;
    00388             }
    00389           }
    00390
    00391           if (_game_mode != GM_EDITOR && IsValidCompanyID(_current_company)) {
    00392             Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
    00393             if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
    00394           }
    00395
    00396           if (flags & DC_EXEC) {
    00397             TreeType treetype;
    00398
    00399             treetype = (TreeType)p1;
    00400             if (treetype == TREE_INVALID) {
    00401               treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
    00402               if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
    00403             }
    00404
    00405             /* Plant full grown trees in scenario editor */
    00406             PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
    00407             MarkTileDirtyByTile(tile);
    00408
    00409             /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
    00410             if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS))
    00411               SetTropicZone(tile, TROPICZONE_RAINFOREST);
    00412           }
    00413           cost.AddCost(_price.build_trees);
    00414           break;
    00415
    00416         default:
    00417           msg = STR_2804_SITE_UNSUITABLE;
    00418           break;
    00419       }
    00420     }
    00421   }
    00422
    00423   if (cost.GetCost() == 0) {
    00424     return_cmd_error(msg);
    00425   } else {
    00426     return cost;
    00427   }
    00428 }
    00429
    00430 struct TreeListEnt {
    00431   SpriteID image;
    00432   SpriteID pal;
    00433   byte x, y;
    00434 };
    00435
    00436 static void DrawTile_Trees(TileInfo *ti)
    00437 {
    00438   switch (GetTreeGround(ti->tile)) {
    00439     case TREE_GROUND_SHORE: DrawShoreTile(ti->tileh); break;
    00440     case TREE_GROUND_GRASS: DrawClearLandTile(ti, GetTreeDensity(ti->tile)); break;
    00441     case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
    00442     default: DrawGroundSprite(_tree_sprites_1[GetTreeDensity(ti->tile)] + _tileh_to_sprite[ti->tileh], PAL_NONE); break;
    00443   }
    00444
    00445   DrawClearLandFence(ti);
    00446
    00447   /* Do not draw trees when the invisible trees setting is set */
    00448   if (IsInvisibilitySet(TO_TREES)) return;
    00449
    00450   uint16 tmp = ti->x;
    00451
    00452   tmp = ROR(tmp, 2);
    00453   tmp -= ti->y;
    00454   tmp = ROR(tmp, 3);
    00455   tmp -= ti->x;
    00456   tmp = ROR(tmp, 1);
    00457   tmp += ti->y;
    00458
    00459   uint index = GB(tmp, 6, 2) + (GetTreeType(ti->tile) << 2);
    00460
    00461   /* different tree styles above one of the grounds */
    00462   if (GetTreeGround(ti->tile) == TREE_GROUND_SNOW_DESERT &&
    00463       GetTreeDensity(ti->tile) >= 2 &&
    00464       IsInsideMM(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
    00465     index += 164 - (TREE_SUB_ARCTIC << 2);
    00466   }
    00467
    00468   assert(index < lengthof(_tree_layout_sprite));
    00469
    00470   const PalSpriteID *s = _tree_layout_sprite[index];
    00471   const TreePos *d = _tree_layout_xy[GB(tmp, 4, 2)];
    00472
    00473   /* combine trees into one sprite object */
    00474   StartSpriteCombine();
    00475
    00476   TreeListEnt te[4];
    00477
    00478   /* put the trees to draw in a list */
    00479   uint trees = GetTreeCount(ti->tile);
    00480
    00481   for (uint i = 0; i < trees; i++) {
    00482     SpriteID image = s[0].sprite + (i == trees - 1 ? GetTreeGrowth(ti->tile) : 3);
    00483     SpriteID pal = s[0].pal;
    00484
    00485     te[i].image = image;
    00486     te[i].pal   = pal;
    00487     te[i].x = d->x;
    00488     te[i].y = d->y;
    00489     s++;
    00490     d++;
    00491   }
    00492
    00493   /* draw them in a sorted way */
    00494   byte z = ti->z + GetSlopeMaxZ(ti->tileh) / 2;
    00495
    00496   for (; trees > 0; trees--) {
    00497     uint min = te[0].x + te[0].y;
    00498     uint mi = 0;
    00499
    00500     for (uint i = 1; i < trees; i++) {
    00501       if ((uint)(te[i].x + te[i].y) < min) {
    00502         min = te[i].x + te[i].y;
    00503         mi = i;
    00504       }
    00505     }
    00506
    00507     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);
    00508
    00509     /* replace the removed one with the last one */
    00510     te[mi] = te[trees - 1];
    00511   }
    00512
    00513   EndSpriteCombine();
    00514 }
    00515
    00516
    00517 static uint GetSlopeZ_Trees(TileIndex tile, uint x, uint y)
    00518 {
    00519   uint z;
    00520   Slope tileh = GetTileSlope(tile, &z);
    00521
    00522   return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
    00523 }
    00524
    00525 static Foundation GetFoundation_Trees(TileIndex tile, Slope tileh)
    00526 {
    00527   return FOUNDATION_NONE;
    00528 }
    00529
    00530 static CommandCost ClearTile_Trees(TileIndex tile, DoCommandFlag flags)
    00531 {
    00532   uint num;
    00533
    00534   if (IsValidCompanyID(_current_company)) {
    00535     Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
    00536     if (t != NULL) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM, flags);
    00537   }
    00538
    00539   num = GetTreeCount(tile);
    00540   if (IsInsideMM(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;
    00541
    00542   if (flags & DC_EXEC) DoClearSquare(tile);
    00543
    00544   return CommandCost(EXPENSES_CONSTRUCTION, num * _price.remove_trees);
    00545 }
    00546
    00547 static void GetAcceptedCargo_Trees(TileIndex tile, AcceptedCargo ac)
    00548 {
    00549   /* not used */
    00550 }
    00551
    00552 static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
    00553 {
    00554   TreeType tt = GetTreeType(tile);
    00555
    00556   if (IsInsideMM(tt, TREE_RAINFOREST, TREE_CACTUS)) {
    00557     td->str = STR_280F_RAINFOREST;
    00558   } else {
    00559     td->str = tt == TREE_CACTUS ? STR_2810_CACTUS_PLANTS : STR_280E_TREES;
    00560   }
    00561
    00562   td->owner[0] = GetTileOwner(tile);
    00563 }
    00564
    00565 static void TileLoopTreesDesert(TileIndex tile)
    00566 {
    00567   switch (GetTropicZone(tile)) {
    00568     case TROPICZONE_DESERT:
    00569       if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
    00570         SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
    00571         MarkTileDirtyByTile(tile);
    00572       }
    00573       break;
    00574
    00575     case TROPICZONE_RAINFOREST: {
    00576       static const SoundFx forest_sounds[] = {
    00577         SND_42_LOON_BIRD,
    00578         SND_43_LION,
    00579         SND_44_MONKEYS,
    00580         SND_48_DISTANT_BIRD
    00581       };
    00582       uint32 r = Random();
    00583
    00584       if (Chance16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
    00585       break;
    00586     }
    00587
    00588     default: break;
    00589   }
    00590 }
    00591
    00592 static void TileLoopTreesAlps(TileIndex tile)
    00593 {
    00594   int k = GetTileZ(tile) - GetSnowLine() + TILE_HEIGHT;
    00595
    00596   if (k < 0) {
    00597     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) return;
    00598     SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3);
    00599   } else {
    00600     uint density = min((uint)k / TILE_HEIGHT, 3);
    00601
    00602     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT ||
    00603         GetTreeDensity(tile) != density) {
    00604       SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, density);
    00605     } else {
    00606       if (GetTreeDensity(tile) == 3) {
    00607         uint32 r = Random();
    00608         if (Chance16I(1, 200, r)) {
    00609           SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
    00610         }
    00611       }
    00612       return;
    00613     }
    00614   }
    00615   MarkTileDirtyByTile(tile);
    00616 }
    00617
    00618 static void TileLoop_Trees(TileIndex tile)
    00619 {
    00620   if (GetTreeGround(tile) == TREE_GROUND_SHORE) {
    00621     TileLoop_Water(tile);
    00622   } else {
    00623     switch (_settings_game.game_creation.landscape) {
    00624       case LT_TROPIC: TileLoopTreesDesert(tile); break;
    00625       case LT_ARCTIC: TileLoopTreesAlps(tile);   break;
    00626     }
    00627   }
    00628
    00629   TileLoopClearHelper(tile);
    00630
    00631   uint treeCounter = GetTreeCounter(tile);
    00632
    00633   /* Handle growth of grass at every 8th processings, like it's done for grass */
    00634   if ((treeCounter & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
    00635     uint density = GetTreeDensity(tile);
    00636     if (density < 3) {
    00637       SetTreeGroundDensity(tile, TREE_GROUND_GRASS, density + 1);
    00638       MarkTileDirtyByTile(tile);
    00639     }
    00640   }
    00641   if (GetTreeCounter(tile) < 15) {
    00642     AddTreeCounter(tile, 1);
    00643     return;
    00644   }
    00645   SetTreeCounter(tile, 0);
    00646
    00647   switch (GetTreeGrowth(tile)) {
    00648     case 3: // regular sized tree
    00649       if (_settings_game.game_creation.landscape == LT_TROPIC &&
    00650           GetTreeType(tile) != TREE_CACTUS &&
    00651           GetTropicZone(tile) == TROPICZONE_DESERT) {
    00652         AddTreeGrowth(tile, 1);
    00653       } else {
    00654         switch (GB(Random(), 0, 3)) {
    00655           case 0: // start destructing
    00656             AddTreeGrowth(tile, 1);
    00657             break;
    00658
    00659           case 1: // add a tree
    00660             if (GetTreeCount(tile) < 4) {
    00661               AddTreeCount(tile, 1);
    00662               SetTreeGrowth(tile, 0);
    00663               break;
    00664             }
    00665             /* FALL THROUGH */
    00666
    00667           case 2: { // add a neighbouring tree
    00668             TreeType treetype = GetTreeType(tile);
    00669
    00670             tile += TileOffsByDir((Direction)(Random() & 7));
    00671
    00672             /* Cacti don't spread */
    00673             if (!CanPlantTreesOnTile(tile, false)) return;
    00674
    00675             /* Don't plant trees, if ground was freshly cleared */
    00676             if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
    00677
    00678             PlantTreesOnTile(tile, treetype, 0, 0);
    00679
    00680             break;
    00681           }
    00682
    00683           default:
    00684             return;
    00685         }
    00686       }
    00687       break;
    00688
    00689     case 6: // final stage of tree destruction
    00690       if (GetTreeCount(tile) > 1) {
    00691         /* more than one tree, delete it */
    00692         AddTreeCount(tile, -1);
    00693         SetTreeGrowth(tile, 3);
    00694       } else {
    00695         /* just one tree, change type into MP_CLEAR */
    00696         switch (GetTreeGround(tile)) {
    00697           case TREE_GROUND_SHORE: MakeShore(tile); break;
    00698           case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, GetTreeDensity(tile)); break;
    00699           case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
    00700           default: // snow or desert
    00701             MakeClear(tile, _settings_game.game_creation.landscape == LT_TROPIC ? CLEAR_DESERT : CLEAR_SNOW, GetTreeDensity(tile));
    00702             break;
    00703         }
    00704       }
    00705       break;
    00706
    00707     default:
    00708       AddTreeGrowth(tile, 1);
    00709       break;
    00710   }
    00711
    00712   MarkTileDirtyByTile(tile);
    00713 }
    00714
    00715 void OnTick_Trees()
    00716 {
    00717   uint32 r;
    00718   TileIndex tile;
    00719   TreeType tree;
    00720
    00721   /* place a tree at a random rainforest spot */
    00722   if (_settings_game.game_creation.landscape == LT_TROPIC &&
    00723       (r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
    00724       CanPlantTreesOnTile(tile, false) &&
    00725       (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
    00726     PlantTreesOnTile(tile, tree, 0, 0);
    00727   }
    00728
    00729   /* byte underflow */
    00730   if (--_trees_tick_ctr != 0) return;
    00731
    00732   /* place a tree at a random spot */
    00733   r = Random();
    00734   tile = RandomTileSeed(r);
    00735   if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
    00736     PlantTreesOnTile(tile, tree, 0, 0);
    00737   }
    00738 }
    00739
    00740 static TrackStatus GetTileTrackStatus_Trees(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    00741 {
    00742   return 0;
    00743 }
    00744
    00745 static void ChangeTileOwner_Trees(TileIndex tile, Owner old_owner, Owner new_owner)
    00746 {
    00747   /* not used */
    00748 }
    00749
    00750 void InitializeTrees()
    00751 {
    00752   _trees_tick_ctr = 0;
    00753 }
    00754
    00755 static CommandCost TerraformTile_Trees(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    00756 {
    00757   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00758 }
    00759
    00760
    00761 extern const TileTypeProcs _tile_type_trees_procs = {
    00762   DrawTile_Trees,           // draw_tile_proc
    00763   GetSlopeZ_Trees,          // get_slope_z_proc
    00764   ClearTile_Trees,          // clear_tile_proc
    00765   GetAcceptedCargo_Trees,   // get_accepted_cargo_proc
    00766   GetTileDesc_Trees,        // get_tile_desc_proc
    00767   GetTileTrackStatus_Trees, // get_tile_track_status_proc
    00768   NULL,                     // click_tile_proc
    00769   NULL,                     // animate_tile_proc
    00770   TileLoop_Trees,           // tile_loop_clear
    00771   ChangeTileOwner_Trees,    // change_tile_owner_clear
    00772   NULL,                     // get_produced_cargo_proc
    00773   NULL,                     // vehicle_enter_tile_proc
    00774   GetFoundation_Trees,      // get_foundation_proc
    00775   TerraformTile_Trees,      // terraform_tile_proc
    00776 };