tree_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: tree_cmd.cpp 18522 2009-12-17 16:59:33Z 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) && !IsClearGround(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;       density = GetClearDensity(tile); break;
00105         case CLEAR_ROUGH:  ground = TREE_GROUND_ROUGH;                                        break;
00106         default:           ground = TREE_GROUND_SNOW_DESERT; density = GetClearDensity(tile); break;
00107       }
00108       break;
00109 
00110     default: NOT_REACHED();
00111   }
00112 
00113   MakeTree(tile, treetype, count, growth, ground, density);
00114 }
00115 
00127 static TreeType GetRandomTreeType(TileIndex tile, uint seed)
00128 {
00129   switch (_settings_game.game_creation.landscape) {
00130     case LT_TEMPERATE:
00131       return (TreeType)(seed * TREE_COUNT_TEMPERATE / 256 + TREE_TEMPERATE);
00132 
00133     case LT_ARCTIC:
00134       return (TreeType)(seed * TREE_COUNT_SUB_ARCTIC / 256 + TREE_SUB_ARCTIC);
00135 
00136     case LT_TROPIC:
00137       switch (GetTropicZone(tile)) {
00138         case TROPICZONE_NORMAL:  return (TreeType)(seed * TREE_COUNT_SUB_TROPICAL / 256 + TREE_SUB_TROPICAL);
00139         case TROPICZONE_DESERT:  return (TreeType)((seed > 12) ? TREE_INVALID : TREE_CACTUS);
00140         default:                 return (TreeType)(seed * TREE_COUNT_RAINFOREST / 256 + TREE_RAINFOREST);
00141       }
00142 
00143     default:
00144       return (TreeType)(seed * TREE_COUNT_TOYLAND / 256 + TREE_TOYLAND);
00145   }
00146 }
00147 
00157 static void PlaceTree(TileIndex tile, uint32 r)
00158 {
00159   TreeType tree = GetRandomTreeType(tile, GB(r, 24, 8));
00160 
00161   if (tree != TREE_INVALID) {
00162     PlantTreesOnTile(tile, tree, GB(r, 22, 2), min(GB(r, 16, 3), 6));
00163 
00164     /* Rerandomize ground, if neither snow nor shore */
00165     TreeGround ground = GetTreeGround(tile);
00166     if (ground != TREE_GROUND_SNOW_DESERT && ground != TREE_GROUND_SHORE) {
00167       SetTreeGroundDensity(tile, (TreeGround)GB(r, 28, 1), 3);
00168     }
00169 
00170     /* Set the counter to a random start value */
00171     SetTreeCounter(tile, (TreeGround)GB(r, 24, 4));
00172   }
00173 }
00174 
00184 static void DoPlaceMoreTrees(TileIndex tile)
00185 {
00186   uint i;
00187 
00188   for (i = 0; i < 1000; i++) {
00189     uint32 r = Random();
00190     int x = GB(r, 0, 5) - 16;
00191     int y = GB(r, 8, 5) - 16;
00192     uint dist = abs(x) + abs(y);
00193     TileIndex cur_tile = TileAddWrap(tile, x, y);
00194 
00195     if (cur_tile != INVALID_TILE && dist <= 13 && CanPlantTreesOnTile(cur_tile, true)) {
00196       PlaceTree(cur_tile, r);
00197     }
00198   }
00199 }
00200 
00206 static void PlaceMoreTrees()
00207 {
00208   uint i = ScaleByMapSize(GB(Random(), 0, 5) + 25);
00209   do {
00210     DoPlaceMoreTrees(RandomTile());
00211   } while (--i);
00212 }
00213 
00223 static void PlaceTreeAtSameHeight(TileIndex tile, uint height)
00224 {
00225   uint i;
00226 
00227   for (i = 0; i < 1000; i++) {
00228     uint32 r = Random();
00229     int x = GB(r, 0, 5) - 16;
00230     int y = GB(r, 8, 5) - 16;
00231     TileIndex cur_tile = TileAddWrap(tile, x, y);
00232     if (cur_tile == INVALID_TILE) continue;
00233 
00234     /* Keep in range of the existing tree */
00235     if (abs(x) + abs(y) > 16) continue;
00236 
00237     /* Clear tile, no farm-tiles or rocks */
00238     if (!CanPlantTreesOnTile(cur_tile, true)) continue;
00239 
00240     /* Not too much height difference */
00241     if (Delta(GetTileZ(cur_tile), height) > 2) continue;
00242 
00243     /* Place one tree and quit */
00244     PlaceTree(cur_tile, r);
00245     break;
00246   }
00247 }
00248 
00254 void PlaceTreesRandomly()
00255 {
00256   uint i, j, ht;
00257 
00258   i = ScaleByMapSize(1000);
00259   do {
00260     uint32 r = Random();
00261     TileIndex tile = RandomTileSeed(r);
00262 
00263     IncreaseGeneratingWorldProgress(GWP_TREE);
00264 
00265     if (CanPlantTreesOnTile(tile, true)) {
00266       PlaceTree(tile, r);
00267       if (_settings_game.game_creation.tree_placer != TP_IMPROVED) continue;
00268 
00269       /* Place a number of trees based on the tile height.
00270        *  This gives a cool effect of multiple trees close together.
00271        *  It is almost real life ;) */
00272       ht = GetTileZ(tile);
00273       /* The higher we get, the more trees we plant */
00274       j = GetTileZ(tile) / TILE_HEIGHT * 2;
00275       while (j--) {
00276         /* Above snowline more trees! */
00277         if (_settings_game.game_creation.landscape == LT_ARCTIC && ht > GetSnowLine()) {
00278           PlaceTreeAtSameHeight(tile, ht);
00279           PlaceTreeAtSameHeight(tile, ht);
00280         };
00281 
00282         PlaceTreeAtSameHeight(tile, ht);
00283       }
00284     }
00285   } while (--i);
00286 
00287   /* place extra trees at rainforest area */
00288   if (_settings_game.game_creation.landscape == LT_TROPIC) {
00289     i = ScaleByMapSize(15000);
00290 
00291     do {
00292       uint32 r = Random();
00293       TileIndex tile = RandomTileSeed(r);
00294 
00295       IncreaseGeneratingWorldProgress(GWP_TREE);
00296 
00297       if (GetTropicZone(tile) == TROPICZONE_RAINFOREST && CanPlantTreesOnTile(tile, false)) {
00298         PlaceTree(tile, r);
00299       }
00300     } while (--i);
00301   }
00302 }
00303 
00310 void GenerateTrees()
00311 {
00312   uint i, total;
00313 
00314   if (_settings_game.game_creation.tree_placer == TP_NONE) return;
00315 
00316   if (_settings_game.game_creation.landscape != LT_TOYLAND) PlaceMoreTrees();
00317 
00318   switch (_settings_game.game_creation.tree_placer) {
00319     case TP_ORIGINAL: i = _settings_game.game_creation.landscape == LT_ARCTIC ? 15 : 6; break;
00320     case TP_IMPROVED: i = _settings_game.game_creation.landscape == LT_ARCTIC ?  4 : 2; break;
00321     default: NOT_REACHED();
00322   }
00323 
00324   total = ScaleByMapSize(1000);
00325   if (_settings_game.game_creation.landscape == LT_TROPIC) total += ScaleByMapSize(15000);
00326   total *= i;
00327   SetGeneratingWorldProgress(GWP_TREE, total);
00328 
00329   for (; i != 0; i--) {
00330     PlaceTreesRandomly();
00331   }
00332 }
00333 
00342 CommandCost CmdPlantTree(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00343 {
00344   StringID msg = INVALID_STRING_ID;
00345   CommandCost cost(EXPENSES_OTHER);
00346   int ex;
00347   int ey;
00348   int sx, sy, x, y;
00349 
00350   if (p2 >= MapSize()) return CMD_ERROR;
00351   /* Check the tree type. It can be random or some valid value within the current climate */
00352   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;
00353 
00354   /* make sure sx,sy are smaller than ex, ey */
00355   ex = TileX(tile);
00356   ey = TileY(tile);
00357   sx = TileX(p2);
00358   sy = TileY(p2);
00359   if (ex < sx) Swap(ex, sx);
00360   if (ey < sy) Swap(ey, sy);
00361 
00362   for (x = sx; x <= ex; x++) {
00363     for (y = sy; y <= ey; y++) {
00364       TileIndex tile = TileXY(x, y);
00365 
00366       switch (GetTileType(tile)) {
00367         case MP_TREES:
00368           /* no more space for trees? */
00369           if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 4) {
00370             msg = STR_ERROR_TREE_ALREADY_HERE;
00371             continue;
00372           }
00373 
00374           if (flags & DC_EXEC) {
00375             AddTreeCount(tile, 1);
00376             MarkTileDirtyByTile(tile);
00377           }
00378           /* 2x as expensive to add more trees to an existing tile */
00379           cost.AddCost(_price[PR_BUILD_TREES] * 2);
00380           break;
00381 
00382         case MP_WATER:
00383           if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL))) {
00384             msg = STR_ERROR_CAN_T_BUILD_ON_WATER;
00385             continue;
00386           }
00387         /* FALL THROUGH */
00388         case MP_CLEAR:
00389           if (IsBridgeAbove(tile)) {
00390             msg = STR_ERROR_SITE_UNSUITABLE;
00391             continue;
00392           }
00393 
00394           if (IsTileType(tile, MP_CLEAR)) {
00395             /* Remove fields or rocks. Note that the ground will get barrened */
00396             switch (GetClearGround(tile)) {
00397               case CLEAR_FIELDS:
00398               case CLEAR_ROCKS: {
00399                 CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00400                 if (CmdFailed(ret)) return ret;
00401                 cost.AddCost(ret);
00402                 break;
00403               }
00404 
00405               default: break;
00406             }
00407           }
00408 
00409           if (_game_mode != GM_EDITOR && Company::IsValidID(_current_company)) {
00410             Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
00411             if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
00412           }
00413 
00414           if (flags & DC_EXEC) {
00415             TreeType treetype;
00416 
00417             treetype = (TreeType)p1;
00418             if (treetype == TREE_INVALID) {
00419               treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
00420               if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
00421             }
00422 
00423             /* Plant full grown trees in scenario editor */
00424             PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
00425             MarkTileDirtyByTile(tile);
00426 
00427             /* When planting rainforest-trees, set tropiczone to rainforest in editor. */
00428             if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS))
00429               SetTropicZone(tile, TROPICZONE_RAINFOREST);
00430           }
00431           cost.AddCost(_price[PR_BUILD_TREES]);
00432           break;
00433 
00434         default:
00435           msg = STR_ERROR_SITE_UNSUITABLE;
00436           break;
00437       }
00438     }
00439   }
00440 
00441   if (cost.GetCost() == 0) {
00442     return_cmd_error(msg);
00443   } else {
00444     return cost;
00445   }
00446 }
00447 
00448 struct TreeListEnt {
00449   SpriteID image;
00450   SpriteID pal;
00451   byte x, y;
00452 };
00453 
00454 static void DrawTile_Trees(TileInfo *ti)
00455 {
00456   switch (GetTreeGround(ti->tile)) {
00457     case TREE_GROUND_SHORE: DrawShoreTile(ti->tileh); break;
00458     case TREE_GROUND_GRASS: DrawClearLandTile(ti, GetTreeDensity(ti->tile)); break;
00459     case TREE_GROUND_ROUGH: DrawHillyLandTile(ti); break;
00460     default: DrawGroundSprite(_clear_land_sprites_snow_desert[GetTreeDensity(ti->tile)] + _tileh_to_sprite[ti->tileh], PAL_NONE); break;
00461   }
00462 
00463   DrawClearLandFence(ti);
00464 
00465   /* Do not draw trees when the invisible trees setting is set */
00466   if (IsInvisibilitySet(TO_TREES)) return;
00467 
00468   uint tmp = CountBits(ti->tile + ti->x + ti->y);
00469   uint index = GB(tmp, 0, 2) + (GetTreeType(ti->tile) << 2);
00470 
00471   /* different tree styles above one of the grounds */
00472   if (GetTreeGround(ti->tile) == TREE_GROUND_SNOW_DESERT &&
00473       GetTreeDensity(ti->tile) >= 2 &&
00474       IsInsideMM(index, TREE_SUB_ARCTIC << 2, TREE_RAINFOREST << 2)) {
00475     index += 164 - (TREE_SUB_ARCTIC << 2);
00476   }
00477 
00478   assert(index < lengthof(_tree_layout_sprite));
00479 
00480   const PalSpriteID *s = _tree_layout_sprite[index];
00481   const TreePos *d = _tree_layout_xy[GB(tmp, 2, 2)];
00482 
00483   /* combine trees into one sprite object */
00484   StartSpriteCombine();
00485 
00486   TreeListEnt te[4];
00487 
00488   /* put the trees to draw in a list */
00489   uint trees = GetTreeCount(ti->tile);
00490 
00491   for (uint i = 0; i < trees; i++) {
00492     SpriteID image = s[0].sprite + (i == trees - 1 ? GetTreeGrowth(ti->tile) : 3);
00493     SpriteID pal = s[0].pal;
00494 
00495     te[i].image = image;
00496     te[i].pal   = pal;
00497     te[i].x = d->x;
00498     te[i].y = d->y;
00499     s++;
00500     d++;
00501   }
00502 
00503   /* draw them in a sorted way */
00504   byte z = ti->z + GetSlopeMaxZ(ti->tileh) / 2;
00505 
00506   for (; trees > 0; trees--) {
00507     uint min = te[0].x + te[0].y;
00508     uint mi = 0;
00509 
00510     for (uint i = 1; i < trees; i++) {
00511       if ((uint)(te[i].x + te[i].y) < min) {
00512         min = te[i].x + te[i].y;
00513         mi = i;
00514       }
00515     }
00516 
00517     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);
00518 
00519     /* replace the removed one with the last one */
00520     te[mi] = te[trees - 1];
00521   }
00522 
00523   EndSpriteCombine();
00524 }
00525 
00526 
00527 static uint GetSlopeZ_Trees(TileIndex tile, uint x, uint y)
00528 {
00529   uint z;
00530   Slope tileh = GetTileSlope(tile, &z);
00531 
00532   return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
00533 }
00534 
00535 static Foundation GetFoundation_Trees(TileIndex tile, Slope tileh)
00536 {
00537   return FOUNDATION_NONE;
00538 }
00539 
00540 static CommandCost ClearTile_Trees(TileIndex tile, DoCommandFlag flags)
00541 {
00542   uint num;
00543 
00544   if (Company::IsValidID(_current_company)) {
00545     Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
00546     if (t != NULL) ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM, flags);
00547   }
00548 
00549   num = GetTreeCount(tile);
00550   if (IsInsideMM(GetTreeType(tile), TREE_RAINFOREST, TREE_CACTUS)) num *= 4;
00551 
00552   if (flags & DC_EXEC) DoClearSquare(tile);
00553 
00554   return CommandCost(EXPENSES_CONSTRUCTION, num * _price[PR_CLEAR_TREES]);
00555 }
00556 
00557 static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
00558 {
00559   TreeType tt = GetTreeType(tile);
00560 
00561   if (IsInsideMM(tt, TREE_RAINFOREST, TREE_CACTUS)) {
00562     td->str = STR_LAI_TREE_NAME_RAINFOREST;
00563   } else {
00564     td->str = tt == TREE_CACTUS ? STR_LAI_TREE_NAME_CACTUS_PLANTS : STR_LAI_TREE_NAME_TREES;
00565   }
00566 
00567   td->owner[0] = GetTileOwner(tile);
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 (_settings_game.game_creation.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 (under trees/on MP_TREES tiles) at every 8th processings, like it's done for grass on MP_CLEAR tiles. */
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 (_settings_game.game_creation.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) < 4) {
00666               AddTreeCount(tile, 1);
00667               SetTreeGrowth(tile, 0);
00668               break;
00669             }
00670             /* FALL THROUGH */
00671 
00672           case 2: { // add a neighbouring tree
00673             /* Don't plant extra trees if that's not allowed. */
00674             if ((_settings_game.game_creation.landscape == LT_TROPIC && GetTropicZone(tile) == TROPICZONE_RAINFOREST) ?
00675                 _settings_game.construction.extra_tree_placement == ETP_NONE :
00676                 _settings_game.construction.extra_tree_placement != ETP_ALL) {
00677               break;
00678             }
00679 
00680             TreeType treetype = GetTreeType(tile);
00681 
00682             tile += TileOffsByDir((Direction)(Random() & 7));
00683 
00684             /* Cacti don't spread */
00685             if (!CanPlantTreesOnTile(tile, false)) return;
00686 
00687             /* Don't plant trees, if ground was freshly cleared */
00688             if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_GRASS && GetClearDensity(tile) != 3) return;
00689 
00690             PlantTreesOnTile(tile, treetype, 0, 0);
00691 
00692             break;
00693           }
00694 
00695           default:
00696             return;
00697         }
00698       }
00699       break;
00700 
00701     case 6: // final stage of tree destruction
00702       if (GetTreeCount(tile) > 1) {
00703         /* more than one tree, delete it */
00704         AddTreeCount(tile, -1);
00705         SetTreeGrowth(tile, 3);
00706       } else {
00707         /* just one tree, change type into MP_CLEAR */
00708         switch (GetTreeGround(tile)) {
00709           case TREE_GROUND_SHORE: MakeShore(tile); break;
00710           case TREE_GROUND_GRASS: MakeClear(tile, CLEAR_GRASS, GetTreeDensity(tile)); break;
00711           case TREE_GROUND_ROUGH: MakeClear(tile, CLEAR_ROUGH, 3); break;
00712           default: // snow or desert
00713             MakeClear(tile, _settings_game.game_creation.landscape == LT_TROPIC ? CLEAR_DESERT : CLEAR_SNOW, GetTreeDensity(tile));
00714             break;
00715         }
00716       }
00717       break;
00718 
00719     default:
00720       AddTreeGrowth(tile, 1);
00721       break;
00722   }
00723 
00724   MarkTileDirtyByTile(tile);
00725 }
00726 
00727 void OnTick_Trees()
00728 {
00729   /* Don't place trees if that's not allowed */
00730   if (_settings_game.construction.extra_tree_placement == ETP_NONE) return;
00731 
00732   uint32 r;
00733   TileIndex tile;
00734   TreeType tree;
00735 
00736   /* place a tree at a random rainforest spot */
00737   if (_settings_game.game_creation.landscape == LT_TROPIC &&
00738       (r = Random(), tile = RandomTileSeed(r), GetTropicZone(tile) == TROPICZONE_RAINFOREST) &&
00739       CanPlantTreesOnTile(tile, false) &&
00740       (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
00741     PlantTreesOnTile(tile, tree, 0, 0);
00742   }
00743 
00744   /* byte underflow */
00745   if (--_trees_tick_ctr != 0 || _settings_game.construction.extra_tree_placement != ETP_ALL) return;
00746 
00747   /* place a tree at a random spot */
00748   r = Random();
00749   tile = RandomTileSeed(r);
00750   if (CanPlantTreesOnTile(tile, false) && (tree = GetRandomTreeType(tile, GB(r, 24, 8))) != TREE_INVALID) {
00751     PlantTreesOnTile(tile, tree, 0, 0);
00752   }
00753 }
00754 
00755 static TrackStatus GetTileTrackStatus_Trees(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00756 {
00757   return 0;
00758 }
00759 
00760 static void ChangeTileOwner_Trees(TileIndex tile, Owner old_owner, Owner new_owner)
00761 {
00762   /* not used */
00763 }
00764 
00765 void InitializeTrees()
00766 {
00767   _trees_tick_ctr = 0;
00768 }
00769 
00770 static CommandCost TerraformTile_Trees(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
00771 {
00772   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00773 }
00774 
00775 
00776 extern const TileTypeProcs _tile_type_trees_procs = {
00777   DrawTile_Trees,           // draw_tile_proc
00778   GetSlopeZ_Trees,          // get_slope_z_proc
00779   ClearTile_Trees,          // clear_tile_proc
00780   NULL,                     // add_accepted_cargo_proc
00781   GetTileDesc_Trees,        // get_tile_desc_proc
00782   GetTileTrackStatus_Trees, // get_tile_track_status_proc
00783   NULL,                     // click_tile_proc
00784   NULL,                     // animate_tile_proc
00785   TileLoop_Trees,           // tile_loop_clear
00786   ChangeTileOwner_Trees,    // change_tile_owner_clear
00787   NULL,                     // add_produced_cargo_proc
00788   NULL,                     // vehicle_enter_tile_proc
00789   GetFoundation_Trees,      // get_foundation_proc
00790   TerraformTile_Trees,      // terraform_tile_proc
00791 };

Generated on Wed Dec 23 23:27:56 2009 for OpenTTD by  doxygen 1.5.6