tree_cmd.cpp

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

Generated on Sun May 15 19:20:17 2011 for OpenTTD by  doxygen 1.6.1