# tree_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: tree_cmd.cpp 15452 2009-02-11 18:50:47Z frosch $ */
    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 = TILE_MASK(tile + TileDiffXY(x, y));
    00178
    00179     if (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 = TILE_MASK(tile + TileDiffXY(x, y));
    00216
    00217     /* Keep in range of the existing tree */
    00218     if (abs(x) + abs(y) > 16) continue;
    00219
    00220     /* Clear tile, no farm-tiles or rocks */
    00221     if (!CanPlantTreesOnTile(cur_tile, true)) continue;
    00222
    00223     /* Not too much height difference */
    00224     if (Delta(GetTileZ(cur_tile), height) > 2) continue;
    00225
    00226     /* Place one tree and quit */
    00227     PlaceTree(cur_tile, r);
    00228     break;
    00229   }
    00230 }
    00231
    00237 void PlaceTreesRandomly()
    00238 {
    00239   uint i, j, ht;
    00240
    00241   i = ScaleByMapSize(1000);
    00242   do {
    00243     uint32 r = Random();
    00244     TileIndex tile = RandomTileSeed(r);
    00245
    00246     IncreaseGeneratingWorldProgress(GWP_TREE);
    00247
    00248     if (CanPlantTreesOnTile(tile, true)) {
    00249       PlaceTree(tile, r);
    00250       if (_settings_game.game_creation.tree_placer != TP_IMPROVED) continue;
    00251
    00252       /* Place a number of trees based on the tile height.
    00253        *  This gives a cool effect of multiple trees close together.
    00254        *  It is almost real life ;) */
    00255       ht = GetTileZ(tile);
    00256       /* The higher we get, the more trees we plant */
    00257       j = GetTileZ(tile) / TILE_HEIGHT * 2;
    00258       while (j--) {
    00259         /* Above snowline more trees! */
    00260         if (_settings_game.game_creation.landscape == LT_ARCTIC && ht > GetSnowLine()) {
    00261           PlaceTreeAtSameHeight(tile, ht);
    00262           PlaceTreeAtSameHeight(tile, ht);
    00263         };
    00264
    00265         PlaceTreeAtSameHeight(tile, ht);
    00266       }
    00267     }
    00268   } while (--i);
    00269
    00270   /* place extra trees at rainforest area */
    00271   if (_settings_game.game_creation.landscape == LT_TROPIC) {
    00272     i = ScaleByMapSize(15000);
    00273
    00274     do {
    00275       uint32 r = Random();
    00276       TileIndex tile = RandomTileSeed(r);
    00277
    00278       IncreaseGeneratingWorldProgress(GWP_TREE);
    00279
    00280       if (GetTropicZone(tile) == TROPICZONE_RAINFOREST && CanPlantTreesOnTile(tile, false)) {
    00281         PlaceTree(tile, r);
    00282       }
    00283     } while (--i);
    00284   }
    00285 }
    00286
    00293 void GenerateTrees()
    00294 {
    00295   uint i, total;
    00296
    00297   if (_settings_game.game_creation.tree_placer == TP_NONE) return;
    00298
    00299   if (_settings_game.game_creation.landscape != LT_TOYLAND) PlaceMoreTrees();
    00300
    00301   switch (_settings_game.game_creation.tree_placer) {
    00302     case TP_ORIGINAL: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 15 : 6; break;
    00303     case TP_IMPROVED: i = _settings_game.game_creation.landscape == LT_ARCTIC ?  4 : 2; break;
    00304     default: NOT_REACHED(); return;
    00305   }
    00306
    00307   total = ScaleByMapSize(1000);
    00308   if (_settings_game.game_creation.landscape == LT_TROPIC) total += ScaleByMapSize(15000);
    00309   total *= i;
    00310   SetGeneratingWorldProgress(GWP_TREE, total);
    00311
    00312   for (; i != 0; i--) {
    00313     PlaceTreesRandomly();
    00314   }
    00315 }
    00316
    00323 CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00324 {
    00325   StringID msg = INVALID_STRING_ID;
    00326   CommandCost cost(EXPENSES_OTHER);
    00327   int ex;
    00328   int ey;
    00329   int sx, sy, x, y;
    00330
    00331   if (p2 >= MapSize()) return CMD_ERROR;
    00332   /* Check the tree type. It can be random or some valid value within the current climate */
    00333   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;
    00334
    00335   // make sure sx,sy are smaller than ex,ey
    00336   ex = TileX(tile);
    00337   ey = TileY(tile);
    00338   sx = TileX(p2);
    00339   sy = TileY(p2);
    00340   if (ex < sx) Swap(ex, sx);
    00341   if (ey < sy) Swap(ey, sy);
    00342
    00343   for (x = sx; x <= ex; x++) {
    00344     for (y = sy; y <= ey; y++) {
    00345       TileIndex tile = TileXY(x, y);
    00346
    00347       switch (GetTileType(tile)) {
    00348         case MP_TREES:
    00349           /* no more space for trees? */
    00350           if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 4) {
    00351             msg = STR_2803_TREE_ALREADY_HERE;
    00352             continue;
    00353           }
    00354
    00355           if (flags & DC_EXEC) {
    00356             AddTreeCount(tile, 1);
    00357             MarkTileDirtyByTile(tile);
    00358           }
    00359           /* 2x as expensive to add more trees to an existing tile */
    00360           cost.AddCost(_price.build_trees * 2);
    00361           break;
    00362
    00363         case MP_WATER:
    00364           if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL))) {
    00365             msg = STR_3807_CAN_T_BUILD_ON_WATER;
    00366             continue;
    00367           }
    00368         /* FALL THROUGH */
    00369         case MP_CLEAR:
    00370           if (IsBridgeAbove(tile)) {
    00371             msg = STR_2804_SITE_UNSUITABLE;
    00372             continue;
    00373           }
    00374
    00375           if (IsTileType(tile, MP_CLEAR)) {
    00376             /* Remove fields or rocks. Note that the ground will get barrened */
    00377             switch (GetClearGround(tile)) {
    00378               case CLEAR_FIELDS:
    00379               case CLEAR_ROCKS: {
    00380                 CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    00381                 if (CmdFailed(ret)) return ret;
    00382                 cost.AddCost(ret);
    00383                 break;
    00384               }
    00385
    00386               default: break;
    00387             }
    00388           }
    00389
    00390           if (_game_mode != GM_EDITOR && IsValidCompanyID(_current_company)) {
    00391             Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
    00392             if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
    00393           }
    00394
    00395           if (flags & DC_EXEC) {
    00396             TreeType treetype;
    00397
    00398             treetype = (TreeType)p1;
    00399             if (treetype == TREE_INVALID) {
    00400               treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
    00401               if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
    00402             }
    00403
    00404             /* Plant full grown trees in scenario editor */
    00405             PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
    00406             MarkTileDirtyByTile(tile);
    00407
    00408             /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
    00409             if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS))
    00410               SetTropicZone(tile, TROPICZONE_RAINFOREST);
    00411           }
    00412           cost.AddCost(_price.build_trees);
    00413           break;
    00414
    00415         default:
    00416           msg = STR_2804_SITE_UNSUITABLE;
    00417           break;
    00418       }
    00419     }
    00420   }
    00421
    00422   if (cost.GetCost() == 0) {
    00423     return_cmd_error(msg);
    00424   } else {
    00425     return cost;
    00426   }
    00427 }
    00428
    00429 struct TreeListEnt {
    00430   SpriteID image;
    00431   SpriteID pal;
    00432   byte x, y;
    00433 };
    00434
    00435 static void DrawTile_Trees(TileInfo *ti)
    00436 {
    00437   switch (GetTreeGround(ti->tile)) {
    00438     case TREE_GROUND_SHORE: DrawShoreTile(ti->tileh); break;
    00439     case TREE_GROUND_GRASS: DrawClearLandTile(ti, GetTreeDensity(ti->tile)); break;
    00440     case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
    00441     default: DrawGroundSprite(_tree_sprites_1[GetTreeDensity(ti->tile)] + _tileh_to_sprite[ti->tileh], PAL_NONE); break;
    00442   }
    00443
    00444   DrawClearLandFence(ti);
    00445
    00446   /* Do not draw trees when the invisible trees setting is set */
    00447   if (IsInvisibilitySet(TO_TREES)) return;
    00448
    00449   uint16 tmp = ti->x;
    00450
    00451   tmp = ROR(tmp, 2);
    00452   tmp -= ti->y;
    00453   tmp = ROR(tmp, 3);
    00454   tmp -= ti->x;
    00455   tmp = ROR(tmp, 1);
    00456   tmp += ti->y;
    00457
    00458   uint index = GB(tmp, 6, 2) + (GetTreeType(ti->tile) << 2);
    00459
    00460   /* different tree styles above one of the grounds */
    00461   if (GetTreeGround(ti->tile) == TREE_GROUND_SNOW_DESERT &&
    00462       GetTreeDensity(ti->tile) >= 2 &&
    00463       IsInsideMM(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
    00464     index += 164 - (TREE_SUB_ARCTIC << 2);
    00465   }
    00466
    00467   assert(index < lengthof(_tree_layout_sprite));
    00468
    00469   const PalSpriteID *s = _tree_layout_sprite[index];
    00470   const TreePos *d = _tree_layout_xy[GB(tmp, 4, 2)];
    00471
    00472   /* combine trees into one sprite object */
    00473   StartSpriteCombine();
    00474
    00475   TreeListEnt te[4];
    00476
    00477   /* put the trees to draw in a list */
    00478   uint trees = GetTreeCount(ti->tile);
    00479
    00480   for (uint i = 0; i < trees; i++) {
    00481     SpriteID image = s[0].sprite + (i == trees - 1 ? GetTreeGrowth(ti->tile) : 3);
    00482     SpriteID pal = s[0].pal;
    00483
    00484     te[i].image = image;
    00485     te[i].pal   = pal;
    00486     te[i].x = d->x;
    00487     te[i].y = d->y;
    00488     s++;
    00489     d++;
    00490   }
    00491
    00492   /* draw them in a sorted way */
    00493   byte z = ti->z + GetSlopeMaxZ(ti->tileh) / 2;
    00494
    00495   for (; trees > 0; trees--) {
    00496     uint min = te[0].x + te[0].y;
    00497     uint mi = 0;
    00498
    00499     for (uint i = 1; i < trees; i++) {
    00500       if ((uint)(te[i].x + te[i].y) < min) {
    00501         min = te[i].x + te[i].y;
    00502         mi = i;
    00503       }
    00504     }
    00505
    00506     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);
    00507
    00508     /* replace the removed one with the last one */
    00509     te[mi] = te[trees - 1];
    00510   }
    00511
    00512   EndSpriteCombine();
    00513 }
    00514
    00515
    00516 static uint GetSlopeZ_Trees(TileIndex tile, uint x, uint y)
    00517 {
    00518   uint z;
    00519   Slope tileh = GetTileSlope(tile, &z);
    00520
    00521   return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
    00522 }
    00523
    00524 static Foundation GetFoundation_Trees(TileIndex tile, Slope tileh)
    00525 {
    00526   return FOUNDATION_NONE;
    00527 }
    00528
    00529 static CommandCost ClearTile_Trees(TileIndex tile, DoCommandFlag flags)
    00530 {
    00531   uint num;
    00532
    00533   if (IsValidCompanyID(_current_company)) {
    00534     Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
    00535     if (t != NULL) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM, flags);
    00536   }
    00537
    00538   num = GetTreeCount(tile);
    00539   if (IsInsideMM(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;
    00540
    00541   if (flags & DC_EXEC) DoClearSquare(tile);
    00542
    00543   return CommandCost(EXPENSES_CONSTRUCTION, num * _price.remove_trees);
    00544 }
    00545
    00546 static void GetAcceptedCargo_Trees(TileIndex tile, AcceptedCargo ac)
    00547 {
    00548   /* not used */
    00549 }
    00550
    00551 static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
    00552 {
    00553   TreeType tt = GetTreeType(tile);
    00554
    00555   if (IsInsideMM(tt, TREE_RAINFOREST, TREE_CACTUS)) {
    00556     td->str = STR_280F_RAINFOREST;
    00557   } else {
    00558     td->str = tt == TREE_CACTUS ? STR_2810_CACTUS_PLANTS : STR_280E_TREES;
    00559   }
    00560
    00561   td->owner[0] = GetTileOwner(tile);
    00562 }
    00563
    00564 static void AnimateTile_Trees(TileIndex tile)
    00565 {
    00566   /* not used */
    00567 }
    00568
    00569 static void TileLoopTreesDesert(TileIndex tile)
    00570 {
    00571   switch (GetTropicZone(tile)) {
    00572     case TROPICZONE_DESERT:
    00573       if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) {
    00574         SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, 3);
    00575         MarkTileDirtyByTile(tile);
    00576       }
    00577       break;
    00578
    00579     case TROPICZONE_RAINFOREST: {
    00580       static const SoundFx forest_sounds[] = {
    00581         SND_42_LOON_BIRD,
    00582         SND_43_LION,
    00583         SND_44_MONKEYS,
    00584         SND_48_DISTANT_BIRD
    00585       };
    00586       uint32 r = Random();
    00587
    00588       if (Chance16I(1, 200, r)) SndPlayTileFx(forest_sounds[GB(r, 16, 2)], tile);
    00589       break;
    00590     }
    00591
    00592     default: break;
    00593   }
    00594 }
    00595
    00596 static void TileLoopTreesAlps(TileIndex tile)
    00597 {
    00598   int k = GetTileZ(tile) - GetSnowLine() + TILE_HEIGHT;
    00599
    00600   if (k < 0) {
    00601     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT) return;
    00602     SetTreeGroundDensity(tile, TREE_GROUND_GRASS, 3);
    00603   } else {
    00604     uint density = min((uint)k / TILE_HEIGHT, 3);
    00605
    00606     if (GetTreeGround(tile) != TREE_GROUND_SNOW_DESERT ||
    00607         GetTreeDensity(tile) != density) {
    00608       SetTreeGroundDensity(tile, TREE_GROUND_SNOW_DESERT, density);
    00609     } else {
    00610       if (GetTreeDensity(tile) == 3) {
    00611         uint32 r = Random();
    00612         if (Chance16I(1, 200, r)) {
    00613           SndPlayTileFx((r & 0x80000000) ? SND_39_HEAVY_WIND : SND_34_WIND, tile);
    00614         }
    00615       }
    00616       return;
    00617     }
    00618   }
    00619   MarkTileDirtyByTile(tile);
    00620 }
    00621
    00622 static void TileLoop_Trees(TileIndex tile)
    00623 {
    00624   if (GetTreeGround(tile) == TREE_GROUND_SHORE) {
    00625     TileLoop_Water(tile);
    00626   } else {
    00627     switch (_settings_game.game_creation.landscape) {
    00628       case LT_TROPIC: TileLoopTreesDesert(tile); break;
    00629       case LT_ARCTIC: TileLoopTreesAlps(tile);   break;
    00630     }
    00631   }
    00632
    00633   TileLoopClearHelper(tile);
    00634
    00635   uint treeCounter = GetTreeCounter(tile);
    00636
    00637   /* Handle growth of grass at every 8th processings, like it's done for grass */
    00638   if ((treeCounter & 7) == 7 && GetTreeGround(tile) == TREE_GROUND_GRASS) {
    00639     uint density = GetTreeDensity(tile);
    00640     if (density < 3) {
    00641       SetTreeGroundDensity(tile, TREE_GROUND_GRASS, density + 1);
    00642       MarkTileDirtyByTile(tile);
    00643     }
    00644   }
    00645   if (GetTreeCounter(tile) < 15) {
    00646     AddTreeCounter(tile, 1);
    00647     return;
    00648   }
    00649   SetTreeCounter(tile, 0);
    00650
    00651   switch (GetTreeGrowth(tile)) {
    00652     case 3: /* regular sized tree */
    00653       if (_settings_game.game_creation.landscape == LT_TROPIC &&
    00654           GetTreeType(tile) != TREE_CACTUS &&
    00655           GetTropicZone(tile) == TROPICZONE_DESERT) {
    00656         AddTreeGrowth(tile, 1);
    00657       } else {
    00658         switch (GB(Random(), 0, 3)) {
    00659           case 0: /* start destructing */
    00660             AddTreeGrowth(tile, 1);
    00661             break;
    00662
    00663           case 1: /* add a tree */
    00664             if (GetTreeCount(tile) < 4) {
    00665               AddTreeCount(tile, 1);
    00666               SetTreeGrowth(tile, 0);
    00667               break;
    00668             }
    00669             /* FALL THROUGH */
    00670
    00671           case 2: { /* add a neighbouring tree */
    00672             TreeType treetype = GetTreeType(tile);
    00673
    00674             tile += TileOffsByDir((Direction)(Random() & 7));
    00675
    00676             /* Cacti don't spread */
    00677             if (!CanPlantTreesOnTile(tile, false)) return;
    00678
    00679             /* Don't plant trees, if ground was freshly cleared */
    00680             if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
    00681
    00682             PlantTreesOnTile(tile, treetype, 0, 0);
    00683
    00684             break;
    00685           }
    00686
    00687           default:
    00688             return;
    00689         }
    00690       }
    00691       break;
    00692
    00693     case 6: /* final stage of tree destruction */
    00694       if (GetTreeCount(tile) > 1) {
    00695         /* more than one tree, delete it */
    00696         AddTreeCount(tile, -1);
    00697         SetTreeGrowth(tile, 3);
    00698       } else {
    00699         /* just one tree, change type into MP_CLEAR */
    00700         switch (GetTreeGround(tile)) {
    00701           case TREE_GROUND_SHORE: MakeShore(tile); break;
    00702           case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, GetTreeDensity(tile)); break;
    00703           case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
    00704           default: // snow or desert
    00705             MakeClear(tile, _settings_game.game_creation.landscape == LT_TROPIC ? CLEAR_DESERT : CLEAR_SNOW, GetTreeDensity(tile));
    00706             break;
    00707         }
    00708       }
    00709       break;
    00710
    00711     default:
    00712       AddTreeGrowth(tile, 1);
    00713       break;
    00714   }
    00715
    00716   MarkTileDirtyByTile(tile);
    00717 }
    00718
    00719 void OnTick_Trees()
    00720 {
    00721   uint32 r;
    00722   TileIndex tile;
    00723   TreeType tree;
    00724
    00725   /* place a tree at a random rainforest spot */
    00726   if (_settings_game.game_creation.landscape == LT_TROPIC &&
    00727       (r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
    00728       CanPlantTreesOnTile(tile, false) &&
    00729       (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
    00730     PlantTreesOnTile(tile, tree, 0, 0);
    00731   }
    00732
    00733   /* byte underflow */
    00734   if (--_trees_tick_ctr != 0) return;
    00735
    00736   /* place a tree at a random spot */
    00737   r = Random();
    00738   tile = TILE_MASK(r);
    00739   if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
    00740     PlantTreesOnTile(tile, tree, 0, 0);
    00741   }
    00742 }
    00743
    00744 static bool ClickTile_Trees(TileIndex tile)
    00745 {
    00746   /* not used */
    00747   return false;
    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, Owner old_owner, Owner new_owner)
    00756 {
    00757   /* not used */
    00758 }
    00759
    00760 void InitializeTrees()
    00761 {
    00762   _trees_tick_ctr = 0;
    00763 }
    00764
    00765 static CommandCost TerraformTile_Trees(TileIndex tile, DoCommandFlag 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 };