# tree_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: tree_cmd.cpp 12199 2008-02-20 17:49:50Z frosch $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "bridge_map.h"
    00008 #include "clear_map.h"
    00009 #include "tile_cmd.h"
    00010 #include "landscape.h"
    00011 #include "tree_map.h"
    00012 #include "viewport_func.h"
    00013 #include "command_func.h"
    00014 #include "economy_func.h"
    00015 #include "town.h"
    00016 #include "variables.h"
    00017 #include "genworld.h"
    00018 #include "transparency.h"
    00019 #include "functions.h"
    00020 #include "player_func.h"
    00021 #include "sound_func.h"
    00022 #include "settings_type.h"
    00023 #include "water_map.h"
    00024 #include "water.h"
    00025
    00026 #include "table/strings.h"
    00027 #include "table/sprites.h"
    00028 #include "table/tree_land.h"
    00029
    00035 enum TreePlacer {
    00036   TP_NONE,
    00037   TP_ORIGINAL,
    00038   TP_IMPROVED,
    00039 };
    00040
    00049 static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
    00050 {
    00051   switch (GetTileType(tile)) {
    00052     case MP_WATER:
    00053       return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL));
    00054
    00055     case MP_CLEAR:
    00056       return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && !IsClearGround(tile, CLEAR_ROCKS) &&
    00057              (allow_desert || !IsClearGround(tile, CLEAR_DESERT));
    00058
    00059     default: return false;
    00060   }
    00061 }
    00062
    00074 static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, uint growth)
    00075 {
    00076   assert(treetype != TREE_INVALID);
    00077   assert(CanPlantTreesOnTile(tile, true));
    00078
    00079   TreeGround ground;
    00080   uint density = 3;
    00081
    00082   switch (GetTileType(tile)) {
    00083     case MP_WATER:
    00084       ground = TREE_GROUND_SHORE;
    00085       break;
    00086
    00087     case MP_CLEAR:
    00088       switch (GetClearGround(tile)) {
    00089         case CLEAR_GRASS:  ground = TREE_GROUND_GRASS;       density = GetClearDensity(tile); break;
    00090         case CLEAR_ROUGH:  ground = TREE_GROUND_ROUGH;                                        break;
    00091         default:           ground = TREE_GROUND_SNOW_DESERT; density = GetClearDensity(tile); break;
    00092       }
    00093       break;
    00094
    00095     default: NOT_REACHED();
    00096   }
    00097
    00098   MakeTree(tile, treetype, count, growth, ground, density);
    00099 }
    00100
    00112 static TreeType GetRandomTreeType(TileIndex tile, uint seed)
    00113 {
    00114   switch (_opt.landscape) {
    00115     case LT_TEMPERATE:
    00116       return (TreeType)(seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE);
    00117
    00118     case LT_ARCTIC:
    00119       return (TreeType)(seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC);
    00120
    00121     case LT_TROPIC:
    00122       switch (GetTropicZone(tile)) {
    00123         case TROPICZONE_NORMAL:  return (TreeType)(seed * TREE_COUNT_SUB_TROPICAL / 256 + TREE_SUB_TROPICAL);
    00124         case TROPICZONE_DESERT:  return (TreeType)((seed > 12) ? TREE_INVALID : TREE_CACTUS);
    00125         default:                 return (TreeType)(seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST);
    00126       }
    00127
    00128     default:
    00129       return (TreeType)(seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND);
    00130   }
    00131 }
    00132
    00142 static void PlaceTree(TileIndex tile, uint32 r)
    00143 {
    00144   TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
    00145
    00146   if (tree != TREE_INVALID) {
    00147     PlantTreesOnTile(tile, tree, GB(r, 22, 2), min(GB(r, 16, 3), 6));
    00148
    00149     /* Rerandomize ground, if neither snow nor shore */
    00150     TreeGround ground = GetTreeGround(tile);
    00151     if (ground != TREE_GROUND_SNOW_DESERT && ground != TREE_GROUND_SHORE) {
    00152       SetTreeGroundDensity(tile, (TreeGround)GB(r, 28, 1), 3);
    00153     }
    00154
    00155     /* Set the counter to a random start value */
    00156     SetTreeCounter(tile, (TreeGround)GB(r, 24, 4));
    00157   }
    00158 }
    00159
    00169 static void DoPlaceMoreTrees(TileIndex tile)
    00170 {
    00171   uint i;
    00172
    00173   for (i = 0; i < 1000; i++) {
    00174     uint32 r = Random();
    00175     int x = GB(r, 0, 5) - 16;
    00176     int y = GB(r, 8, 5) - 16;
    00177     uint dist = abs(x) + abs(y);
    00178     TileIndex cur_tile = TILE_MASK(tile + TileDiffXY(x, y));
    00179
    00180     if (dist <= 13 && CanPlantTreesOnTile(cur_tile, true)) {
    00181       PlaceTree(cur_tile, r);
    00182     }
    00183   }
    00184 }
    00185
    00191 static void PlaceMoreTrees()
    00192 {
    00193   uint i = ScaleByMapSize(GB(Random(), 0, 5) + 25);
    00194   do {
    00195     DoPlaceMoreTrees(RandomTile());
    00196   } while (--i);
    00197 }
    00198
    00208 static void PlaceTreeAtSameHeight(TileIndex tile, uint height)
    00209 {
    00210   uint i;
    00211
    00212   for (i = 0; i < 1000; i++) {
    00213     uint32 r = Random();
    00214     int x = GB(r, 0, 5) - 16;
    00215     int y = GB(r, 8, 5) - 16;
    00216     TileIndex cur_tile = TILE_MASK(tile + TileDiffXY(x, y));
    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 (_patches.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 (_opt.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 (_opt.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 (_patches.tree_placer == TP_NONE) return;
    00299
    00300   if (_opt.landscape != LT_TOYLAND) PlaceMoreTrees();
    00301
    00302   switch (_patches.tree_placer) {
    00303     case TP_ORIGINAL: i = _opt.landscape == LT_ARCTIC ? 15 : 6; break;
    00304     case TP_IMPROVED: i = _opt.landscape == LT_ARCTIC ?  4 : 2; break;
    00305     default: NOT_REACHED(); return;
    00306   }
    00307
    00308   total = ScaleByMapSize(1000);
    00309   if (_opt.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, uint32 flags, uint32 p1, uint32 p2)
    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)-1 && p1 - _tree_base_by_landscape[_opt.landscape] >= _tree_count_by_landscape[_opt.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) == 3) {
    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 && IsValidPlayer(_current_player)) {
    00392             Town *t = ClosestTownFromTile(tile, _patches.dist_local_authority);
    00393             if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM);
    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 patch and transparency tree are set */
    00448   if (IsTransparencySet(TO_TREES) && _patches.invisible_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) + 1;
    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, byte flags)
    00531 {
    00532   uint num;
    00533
    00534   if (IsValidPlayer(_current_player)) {
    00535     Town *t = ClosestTownFromTile(tile, _patches.dist_local_authority);
    00536     if (t != NULL) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM);
    00537   }
    00538
    00539   num = GetTreeCount(tile) + 1;
    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 = GetTileOwner(tile);
    00563 }
    00564
    00565 static void AnimateTile_Trees(TileIndex tile)
    00566 {
    00567   /* not used */
    00568 }
    00569
    00570 static void TileLoopTreesDesert(TileIndex tile)
    00571 {
    00572   switch (GetTropicZone(tile)) {
    00573     case TROPICZONE_DESERT:
    00574       if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
    00575         SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
    00576         MarkTileDirtyByTile(tile);
    00577       }
    00578       break;
    00579
    00580     case TROPICZONE_RAINFOREST: {
    00581       static const SoundFx forest_sounds[] = {
    00582         SND_42_LOON_BIRD,
    00583         SND_43_LION,
    00584         SND_44_MONKEYS,
    00585         SND_48_DISTANT_BIRD
    00586       };
    00587       uint32 r = Random();
    00588
    00589       if (Chance16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
    00590       break;
    00591     }
    00592
    00593     default: break;
    00594   }
    00595 }
    00596
    00597 static void TileLoopTreesAlps(TileIndex tile)
    00598 {
    00599   int k = GetTileZ(tile) - GetSnowLine() + TILE_HEIGHT;
    00600
    00601   if (k < 0) {
    00602     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) return;
    00603     SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3);
    00604   } else {
    00605     uint density = min((uint)k / TILE_HEIGHT, 3);
    00606
    00607     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT ||
    00608         GetTreeDensity(tile) != density) {
    00609       SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, density);
    00610     } else {
    00611       if (GetTreeDensity(tile) == 3) {
    00612         uint32 r = Random();
    00613         if (Chance16I(1, 200, r)) {
    00614           SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
    00615         }
    00616       }
    00617       return;
    00618     }
    00619   }
    00620   MarkTileDirtyByTile(tile);
    00621 }
    00622
    00623 static void TileLoop_Trees(TileIndex tile)
    00624 {
    00625   if (GetTreeGround(tile) == TREE_GROUND_SHORE) {
    00626     TileLoop_Water(tile);
    00627   } else {
    00628     switch (_opt.landscape) {
    00629       case LT_TROPIC: TileLoopTreesDesert(tile); break;
    00630       case LT_ARCTIC: TileLoopTreesAlps(tile);   break;
    00631     }
    00632   }
    00633
    00634   TileLoopClearHelper(tile);
    00635
    00636   uint treeCounter = GetTreeCounter(tile);
    00637
    00638   /* Handle growth of grass at every 8th processings, like it's done for grass */
    00639   if ((treeCounter & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
    00640     uint density = GetTreeDensity(tile);
    00641     if (density < 3) {
    00642       SetTreeGroundDensity(tile, TREE_GROUND_GRASS, density + 1);
    00643       MarkTileDirtyByTile(tile);
    00644     }
    00645   }
    00646   if (GetTreeCounter(tile) < 15) {
    00647     AddTreeCounter(tile, 1);
    00648     return;
    00649   }
    00650   SetTreeCounter(tile, 0);
    00651
    00652   switch (GetTreeGrowth(tile)) {
    00653     case 3: /* regular sized tree */
    00654       if (_opt.landscape == LT_TROPIC &&
    00655           GetTreeType(tile) != TREE_CACTUS &&
    00656           GetTropicZone(tile) == TROPICZONE_DESERT) {
    00657         AddTreeGrowth(tile, 1);
    00658       } else {
    00659         switch (GB(Random(), 0, 3)) {
    00660           case 0: /* start destructing */
    00661             AddTreeGrowth(tile, 1);
    00662             break;
    00663
    00664           case 1: /* add a tree */
    00665             if (GetTreeCount(tile) < 3) {
    00666               AddTreeCount(tile, 1);
    00667               SetTreeGrowth(tile, 0);
    00668               break;
    00669             }
    00670             /* FALL THROUGH */
    00671
    00672           case 2: { /* add a neighbouring tree */
    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) > 0) {
    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           default: // snow or desert
    00706             MakeClear(tile, _opt.landscape == LT_TROPIC ? CLEAR_DESERT : CLEAR_SNOW, GetTreeDensity(tile));
    00707             break;
    00708         }
    00709       }
    00710       break;
    00711
    00712     default:
    00713       AddTreeGrowth(tile, 1);
    00714       break;
    00715   }
    00716
    00717   MarkTileDirtyByTile(tile);
    00718 }
    00719
    00720 void OnTick_Trees()
    00721 {
    00722   uint32 r;
    00723   TileIndex tile;
    00724   TreeType tree;
    00725
    00726   /* place a tree at a random rainforest spot */
    00727   if (_opt.landscape == LT_TROPIC &&
    00728       (r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
    00729       CanPlantTreesOnTile(tile, false) &&
    00730       (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
    00731     PlantTreesOnTile(tile, tree, 0, 0);
    00732   }
    00733
    00734   /* byte underflow */
    00735   if (--_trees_tick_ctr != 0) return;
    00736
    00737   /* place a tree at a random spot */
    00738   r = Random();
    00739   tile = TILE_MASK(r);
    00740   if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
    00741     PlantTreesOnTile(tile, tree, 0, 0);
    00742   }
    00743 }
    00744
    00745 static void ClickTile_Trees(TileIndex tile)
    00746 {
    00747   /* not used */
    00748 }
    00749
    00750 static TrackStatus GetTileTrackStatus_Trees(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    00751 {
    00752   return 0;
    00753 }
    00754
    00755 static void ChangeTileOwner_Trees(TileIndex tile, PlayerID old_player, PlayerID new_player)
    00756 {
    00757   /* not used */
    00758 }
    00759
    00760 void InitializeTrees()
    00761 {
    00762   _trees_tick_ctr = 0;
    00763 }
    00764
    00765 static CommandCost TerraformTile_Trees(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
    00766 {
    00767   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00768 }
    00769
    00770
    00771 extern const TileTypeProcs _tile_type_trees_procs = {
    00772   DrawTile_Trees,           /* draw_tile_proc */
    00773   GetSlopeZ_Trees,          /* get_slope_z_proc */
    00774   ClearTile_Trees,          /* clear_tile_proc */
    00775   GetAcceptedCargo_Trees,   /* get_accepted_cargo_proc */
    00776   GetTileDesc_Trees,        /* get_tile_desc_proc */
    00777   GetTileTrackStatus_Trees, /* get_tile_track_status_proc */
    00778   ClickTile_Trees,          /* click_tile_proc */
    00779   AnimateTile_Trees,        /* animate_tile_proc */
    00780   TileLoop_Trees,           /* tile_loop_clear */
    00781   ChangeTileOwner_Trees,    /* change_tile_owner_clear */
    00782   NULL,                     /* get_produced_cargo_proc */
    00783   NULL,                     /* vehicle_enter_tile_proc */
    00784   GetFoundation_Trees,      /* get_foundation_proc */
    00785   TerraformTile_Trees,      /* terraform_tile_proc */
    00786 };