tree_cmd.cpp

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